Python Forum
Trouble converting JSON String to Dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble converting JSON String to Dictionary
#1
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())
Reply
#2
Not sure, but this might work:
dictname = json.loads(restResponse.json())
Reply
#3
Larrz60+

Thanks, tried it, thows: "TypeError: the JSON object must be str, bytes or bytearray, not dict"
Reply
#4
you are using requests, so Response.json() will give you json content decoded
in order to help you, post the full json you get.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Hello and thanks hereis the JSON i am getting
dict_values([[{'Id': '0f94fcef-0018-47a8-a0e7-7940012d6c27', 'originid': 1130793332, 'userid': 'u28635272', 'targetid': 1222129914
Reply
#6
(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()
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
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())
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  trouble reading string/module from excel as a list popular_dog 0 414 Oct-04-2023, 01:07 PM
Last Post: popular_dog
  [split] Parse Nested JSON String in Python mmm07 4 1,515 Mar-28-2023, 06:07 PM
Last Post: snippsat
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,367 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  Converting cells in excel to JSON format desmondtay 4 1,721 May-23-2022, 10:31 AM
Last Post: Larz60+
  Converting '1a2b3c' string to Dictionary PythonNoobLvl1 6 1,839 May-13-2022, 03:44 PM
Last Post: deanhystad
  string indices must be integers when parsing Json ilknurg 3 6,323 Mar-10-2022, 11:02 AM
Last Post: DeaD_EyE
Question How do I skipkeys on json file read to python dictionary? BrandonKastning 3 1,878 Mar-08-2022, 09:34 PM
Last Post: BrandonKastning
  Need help converting string to int dedesssse 7 2,672 Jul-07-2021, 09:32 PM
Last Post: deanhystad
Question convert unlabeled list of tuples to json (string) masterAndreas 4 7,442 Apr-27-2021, 10:35 AM
Last Post: masterAndreas
  Beautify dictionary without converting to string. sharoon 6 3,356 Apr-11-2021, 08:32 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020