Search code examples
pythonjsonvariablesselectfetch

How to get list of values of a single key from JSON using Python


I have these list, and I want to get the value of name:

[{
  "tr": [{
      "name": "#For5Rs",
      "promoted_content": null,
      "query": "%23For5Rs",
      "events": null
  }, {
      "name": "Javed Bashir",
      "promoted_content": null,
      "query": "%22Javed+Bashir%22",
      "events": null
  }, {
      "name": "Milan Luthria",
      "promoted_content": null,
      "query": "%22Milan+Luthria%22",
      "events": null
  }, {
      "name": "Vidya Balan",
      "promoted_content": null,
      "query": "%22Vidya+Balan%22",
      "events": null
  }],
  "as_of": "2013-08-16T10:31:35Z",
  "created_at": "2013-08-16T10:20:41Z",
  "locations": [{
      "name": "India",
      "woeid": 23424848
  }]
}]

Solution

  • for element in data['tr']:
        print(element['name'])
        print(element['promoted_content'])
        print(element['query'])
        print(element['events'])