Python Forum

Full Version: Trouble converting JSON String to Dictionary
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello All:

I am fairly new to python, so

I have a small app that pulls data from REST using requests this returns a response object that I convert to JSON with this
jData = restResponse.json() #Convert to JSON
At this point I want to do something like this
for value in jData.items():
    print(jData['alias']) #Throws key not found error.
#If I do this however it runs, the problem is it seems to be printing everything out as one long string, I can;t access individual values using something like
jData['SomeKey'}
I think its something simple but for the life of me I cant figure what
Any Ideas/ Thanks Much!
print(jData.values())
Not sure, but this might work:
dictname = json.loads(restResponse.json())
Larrz60+

Thanks, tried it, thows: "TypeError: the JSON object must be str, bytes or bytearray, not dict"
you are using requests, so Response.json() will give you json content decoded
in order to help you, post the full json you get.
Hello and thanks hereis the JSON i am getting
dict_values([[{'Id': '0f94fcef-0018-47a8-a0e7-7940012d6c27', 'originid': 1130793332, 'userid': 'u28635272', 'targetid': 1222129914
(Mar-26-2019, 09:25 PM)RBeck22 Wrote: [ -> ]Hello and thanks hereis the JSON i am getting
dict_values([[{'Id': '0f94fcef-0018-47a8-a0e7-7940012d6c27', 'originid': 1130793332, 'userid': 'u28635272', 'targetid': 1222129914

I deal with JSON very occasionally, but it seems to me that there are too many square brackets. If I query some statistical data then values are usually:

dict_values([{'id': '1d8b380f-4e34-4fb1-8cfd-a459eeee89a0', 'test': False, 'prepared': '2019-03-27T07:26:22.6786715Z...
You can try jData[0].values()
Please post the full output from
print(restResponse.json())
or
print(jData)
at the moment you post just a partial output from
print(restResponse.json().values())
or
print(jData.values())
Hello All, and Thanks!!!

I got it partially solved:
for i in x:
x['key'][0]['other'])
x['key'][1]['blahblah'])

Anyway thanks for the help!