1

When you have a JSON response that contains multiple JSON objects, how do you pull out a specific object within the JSON using Python?

For example, with the following JSON response, I have three objects in it.

{
"_links": {
    "base": "REDACTED", 
    "context": "", 
    "self": "REDACTED"
}, 
"limit": 20, 
"results": [
    {
        "_expandable": {
            "ancestors": "", 
            "body": "", 
            "children": "", 
            "container": "", 
            "descendants": "", 
            "extensions": "", 
            "history": "/rest/api/content/198121503/history", 
            "metadata": "", 
            "operations": "", 
            "space": "/rest/api/space/ReleaseNotes", 
            "version": ""
        }, 
        "_links": {
            "self": "REDACTED", 
            "tinyui": "/x/HxjPCw", 
            "webui": "UNIQUE_URL_HERE"
        }, 
        "id": "198121503", 
        "status": "current", 
        "title": "Unique Title of Content", 
        "type": "page"
    }, 
    {
        "_expandable": {
            "ancestors": "", 
            "body": "", 
            "children": "", 
            "container": "", 
            "descendants": "", 
            "extensions": "", 
            "history": "/rest/api/content/197195923/history", 
            "metadata": "", 
            "operations": "", 
            "space": "/rest/api/space/ReleaseNotes", 
            "version": ""
        }, 
        "_links": {
            "self": "REDACTED", 
            "tinyui": "/x/k-jACw", 
            "webui": "UNIQUE_URL_HERE"
        }, 
        "id": "197195923", 
        "status": "current", 
        "title": "Unique Title of Content", 
        "type": "page"
    }, 
    {
        "_expandable": {
            "ancestors": "", 
            "body": "", 
            "children": "", 
            "container": "", 
            "descendants": "", 
            "extensions": "", 
            "history": "/rest/api/content/198121203/history", 
            "metadata": "", 
            "operations": "", 
            "space": "/rest/api/space/ReleaseNotes", 
            "version": ""
        }, 
        "_links": {
            "self": "REDACTED", 
            "tinyui": "/x/8xbPCw", 
            "webui": "UNIQUE_URL_HERE"
        }, 
        "id": "198121203", 
        "status": "current", 
        "title": "Unique Title of Content", 
        "type": "page"
    } 
], 
"size": 3, 
"start": 0

}

How can I retrieve the ID and TITLE for a specific object in the response?

I read in other threads that when you use json.loads(your_json), it becomes a dictionary. If that's the case, how do I pull this data if it's stored as a dictionary?

Update

Let me clarify, as maybe I'm not seeing or explaining this clearly.

Is the only option to cycle through everything? There's not an option to say get me the 2nd JSON object and return the ID and Title? If that's the case, why shouldn't I create a custom object, store the items I want from each JSON object into those within an array, then I can access each object within the array?

TW_Doc
  • 49
  • 1
  • 2
  • 6
  • I'm really not sure who plussed this, but your question is, "How do I access stuff from a `dict`?" This is *basic* info any standard Python introduction would cover. – jpmc26 Jul 12 '16 at 17:08

2 Answers2

1

After you transform your response to json, you can just use key attributes.

for result in data['results']:
    print("id: {}, title: {}".format(result['id'], result['title']))

As you mentioned, you can use json.load to transform string to dictionary. But if you're using requests library, just use response.json to get data in required format.

grundic
  • 4,641
  • 3
  • 31
  • 47
1

Use bracket notation to access the keys after loading the string into a json object. Loop through the results key until you find the object you want, just like this:

j = json.loads(your_json)

for r in j["results"]:
    if r["title"] == "Something":
        print(r["id"])
        print(r["title"])
CamJohnson26
  • 1,119
  • 1
  • 15
  • 40