简体   繁体   中英

Index JSON searches python

I have the next JSON that I get from a URL:

[{
  "id": 1,
  "version": 23,
  "external_id": "2312",
  "url": "https://example.com/432",
  "type": "typeA",
  "date": "2",
  "notes": "notes",
  "title": "title",
  "abstract": "dsadasdas",
  "details": "something",
  "accuracy": 0,
  "reliability": 0,
  "severity": 12,
  "thing": "32132",
  "other": [
    "aaaaaaaaaaaaaaaaaa",
    "bbbbbbbbbbbbbb",
    "cccccccccccccccc",
    "dddddddddddddd",
    "eeeeeeeeee"
  ],
  "nana": 8
},
{
  "id": 2,
  "version": 23,
  "external_id": "2312",
  "url": "https://example.com/432",
  "type": "typeA",
  "date": "2",
  "notes": "notes",
  "title": "title",
  "abstract": "dsadasdas",
  "details": "something",
  "accuracy": 0,
  "reliability": 0,
  "severity": 12,
  "thing": "32132",
  "other": [
    "aaaaaaaaaaaaaaaaaa",
    "bbbbbbbbbbbbbb",
    "cccccccccccccccc",
    "dddddddddddddd",
    "eeeeeeeeee"
  ],
  "nana": 8
}]

My code:

import json
import urllib2

data = json.load(urllib2.urlopen('http://someurl/path/to/json'))
print data

I want to know how to access to the part "abstract" of the object that has "id" equal to 2 for example. The part "id" is unique so I can use id to index my searchs.

Thanks!

Here's one way to do it. You can create a generator via a generator expression, call next to iterate that generator once, and get back the desired object.

item = next((item for item in data if item['id'] == 2), None)
if item:
    print item['abstract']

See also Python: get a dict from a list based on something inside the dict

EDIT : If you'd like access to all elements of the list that have a given key value (for example, id == 2 ) you can do one of two things. You can either create a list via comprehension (as shown in the other answer), or you can alter my solution:

my_gen = (item for item in data if item['id'] == 2)
for item in my_gen:
    print item

In the loop, item will iterate over those items in your list which satisfy the given condition (here, id == 2 ).

You can use list comprehention to filter:

import json

j = """[{"id":1,"version":23,"external_id":"2312","url":"https://example.com/432","type":"typeA","date":"2","notes":"notes","title":"title","abstract":"dsadasdas","details":"something","accuracy":0,"reliability":0,"severity":12,"thing":"32132","other":["aaaaaaaaaaaaaaaaaa","bbbbbbbbbbbbbb","cccccccccccccccc","dddddddddddddd","eeeeeeeeee"],"nana":8},{"id":2,"version":23,"external_id":"2312","url":"https://example.com/432","type":"typeA","date":"2","notes":"notes","title":"title","abstract":"dsadasdas","details":"something","accuracy":0,"reliability":0,"severity":12,"thing":"32132","other":["aaaaaaaaaaaaaaaaaa","bbbbbbbbbbbbbb","cccccccccccccccc","dddddddddddddd","eeeeeeeeee"],"nana":8}]"""

dicto = json.loads(j)

results = [x for x in dicto if "id" in x and x["id"]==2]

And then you can print the 'abstract' values like so:

for result in results:
    if "abstract" in result:
        print result["abstract"]
import urllib2
import json
data = json.load(urllib2.urlopen('http://someurl/path/to/json'))
your_id = raw_input('enter the id')
for each in data:
    if each['id'] == your_id:
        print each['abstract']

In the above code data is list and each is a dict you can easily access the dict object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM