2

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!

1

3 Answers 3

3

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).

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. Is there any way to do something like this: data["id=2"]["abstract"] ?? Thanks again
Python doesn't support deep key lookups as you've shown, but you can simulate them using the above.
The problem is that if I want to show all the objects that have the "date" = 2 it only show me one object instead of both of them
Please see my updated answer addressing this concern.
@Alberto: To get a list of the details fields from all the objects with 'id' == 2 use [obj['details'] for obj in data if obj['id'] == 2].
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"]

Comments

0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.