Home > database >  I can't get a value from a JSON API response in python
I can't get a value from a JSON API response in python

Time:12-07

So I am struggling with getting a value from a JSON response. Looking in other post I have managed to write this code but when I try to search for the key (character_id) that I want in the dictionary python says that the key doesn't exist. My solution consists in getting the JSON object from the response, converting it into a string with json.dumps() and the converting it into a dictionary with json.loads(). Then I try to get 'character_id' from the dictionary but it doesn't exist. I am guessing it is related with the format of the dictionary but I have little to none experience in python. The code that makes the query and tries to get the values is this: (dataRequest is a fuction that makes the request and return the response from the api)

characterName = sys.argv[1];
response = dataRequest('http://census.daybreakgames.com/s:888/get/ps2:v2/character/?name.first_lower='   characterName   '&c:show=character_id')
jsonString = json.dumps(response.json())
print(jsonString)
dic = json.loads(jsonString)
print(dic)
if 'character_id' in dic:
    print(dic['character_id'])

The output of the code is:

{"character_list": [{"character_id": "5428662532301799649"}], "returned": 1}
{'character_list': [{'character_id': '5428662532301799649'}], 'returned': 1}

CodePudding user response:

Welcome @Prieto! From what I can see, you probably don't need to serialize/de-serialize the JSON -- response.json() returns a python dictionary object already.

The issue is that you are looking for the 'character_id' key at the top-level of the dictionary, when it seems to be embedded inside another dictionary, that is inside a list. Try something like this:

#...omitted code

for char_obj in dic["character_list"]:
    if "character_id" in char_obj:
        print(char_obj["character_id"])

CodePudding user response:

if your dic is like {"character_list": [{"character_id": "5428662532301799649"}], "returned": 1}

you get the value of character_id by

print(dic['character_list'][0][character_id])

CodePudding user response:

The problem here is that you're trying to access a dictionary where the key is actually character_list. What you need to do is to access the character_list value and iterate over or filter the character_id you want.

Like this:

print(jsonString)
dic = json.loads(jsonString)
print(dic)
character_information = dic['character_list'][0] # we access the character list and assume it is the first value
print(character_information["character_id"]) # this is your character id

CodePudding user response:

The way I see it, the only hiccup with the code is this :

if 'character_id' in dic:
    print(dic['character_id'])

The problem is that, the JSON file actually consists of actually 2 dictionaries , first is the main one, which has two keys, character_list and returned. There is a second sub-dictionary inside the array, which is the value for the key character_list. So, what your code should actually look like is something like this:

for i in dic["character_list"]:
    print(i["character_id"])

On a side-note, it will help to look at JSON file in this way :

{
    "character_list": [
        {
            "character_id": "5428662532301799649"
        }
    ],
    "returned": 1
}

,where, elements enclosed in curly-brackets'{}' imply they are in a dictionary, whereas elements enclosed in curly-brackets'[]' imply they are in a list

  • Related