[Fixed]-How to use captured value from URL to select JSON data in Django

1👍

This is how to get it in python:

data = [

  {
    "id": 1,
    "value": 123,
    "value2": 123123
  },
  {
    "id": 2,
    "value": 1,
    "value2": 214
  }

]
result = None
for item in data:
    if item['value'] == 123:
       result = [item]
       break

print(result)
👤ruddra

0👍

Here is a javascript code to do this :-

function test()
{
//alert("Hi");
var text = '[{"id":1,"value":123,"value2":123123},{"id":2,"value":1,"value2":214}]'

json = JSON.parse(text);

var result = [];

for(var i = 0; i < json.length; i++) {
    var obj = json[i];
    if(obj['value'] == 123){
        result.push(obj);
    }

}

console.log(result);

}
👤Max08

0👍

In views.py , you can get the value of the URL through request object

    data = [

  {
    "id": 1,
    "value": 123,
    "value2": 123123
  },
  {
    "id": 2,
    "value": 1,
    "value2": 214
  }

]
print [ item for item in data if item['value'] == request.GET.get('value') ]

Leave a comment