1

I'm making a request call with python but I'm struggling to retrieve the data that I need from the Json.

here the Json:

{
    "count": 987,
    "value": [
        {
            "quality": "definition",
            "drafts": [],
            "queue": {
                "_links": {
                    "self": {
                        "href": "https://myUrl/"
                    }
                },
                "id": 987551,
                "name": "something",
                "url": "https://myUrl/",
                "pool": {
                    "id": 81,
                    "name": "myProjectName"
                }
            },
            "id": 477,
            "name": "myProjectName",
            "url": "https://myUrl/",
            "uri": "",
            "path": "\\",
            "type": "build",
            "queueStatus": "enabled",
            "revision": 18,
            "createdDate": "2020-07-03T09:06:01.6Z"
        },

        {
            //another object similar to the above one

        },
...
    ]
}

and this is what I'm trying:

response = requests.get(organization_url, auth=basic, headers=headers)

test = response.json()
print(test['value'][0])

with the code above I'm able to print the first object of my value, and I have no problem accessing the "queue" object using something like

print(test['value'][0]['queue'])

But I cannot understand how I can print the id or the name tha are outside the queue object

            "id": 477,
            "name": "myPersonalProjectName",

If I try something

print(test['value'][0]['name'])

I get:

KeyError: 'name'

I have also tried to iterate but I'm missing something. From my understanding, the value object should be a list with different dict inside and others key:value.

But why something like this doesn't work?

print(test['value'][0]['name'])

Thanks!

Car_mine
  • 71
  • 1
  • 1
  • 7
  • 1
    I can't reproduce the error. `print(test['value'][0]['name'])` outputs `myProjectName` for me. Please provide more information on how to reproduce this error. – Michael Ruth Aug 14 '23 at 22:36

2 Answers2

0

Because we're not able to reproduce the error again, I was working on a JsonDF package, and I was makign a JSON type in it, which automatically converts the Json to an Onject, and when I added your Json in it, I was able to access to all the attributes in it, even the one you've mentioned that reproduce an error.

and as @Michael Ruth in a comment, we can't use a dot . to access keys and values because . doesn't supports in dict.

in the Json type in JsonDF you can access whatever values you want with . same as JQuery, try it and if you faced any problem tell me or add it in issues in GitHub.

install it from pip install JsonDF==1.1.0

or from GitHub

and try it

from JsonDF.utils.Json import json

json = Json(json=some_josn_goes_here, name=name_your_object)
json.objectify()
print(json.value[0].name, json.value[0].id)

outputs

myProjectName 477
0

Thanks all for your reply guys,

as always it was my bad! It seems that somehow I took the wrong Json from the request that I was doing. And python was right, there isn't a key called name in my Json, or there is, but it's more nested.

So, something like

print(test['value'][0]['name'])

is not working because in correct JSON, at position [0], 'name' is not there, but

print(test['value'][0]['definition']['name'])

I'm almost sure that I copied the Json wrongly, and I could not figure out why my script was not working. So I made a call with postman, and start searching for the field that I was looking for, and then I found that it was more nested.

Thanks!

Car_mine
  • 71
  • 1
  • 1
  • 7