Home > Enterprise >  When loading a JSON body receiving error: "expecting ',' delimiter: line 1 column 18
When loading a JSON body receiving error: "expecting ',' delimiter: line 1 column 18

Time:04-03

import json
a_json = '{"some_body":" 
[{"someId":"189353945391","EId":"09358039485","someUID":10,"LegalId":"T743","cDate":"202452","rAmount":{"aPa":{"am":1500,"currId":"UD"},"cost":{"amount":1000,"currId":"US"},"lPrice":{"amount":100,"currId":"DD"}},"tes":{"ant":0,"currId":"US"},"toount":{"amnt":0,"currId":"US"},"toount":{"amt":210,"currId":"US"},"bry":"US","pay":[{"pId":"7111","axt":{"amt":2000,"currId":"US"},"mKey":"CSD"}],"oItems":[{"iIndex":0,"rId":"69823","provId":"001","segEntityId":"C001","per":{"vae":1,"ut":"MOS"},"pct":{"prod":"748"},"revType":"REW","rAmount":{"aPaid":{"amt":90000,"currId":"US"},"xt":{"amt":0,"currId":"USD"},"lPrice":{"amt":90000,"currId":"US"}},"stion":{"sLocal":"094u5304","eLocal":"3459340"},"tx":{"adt":{"adet":0,"currId":"US"},"era":"werTIC"}}}]"}'

Above is the JSON I am trying to iterate through. Kind of a weird JSON. I want to take that JSON string and turn it into a python dictionary, so I can better iterate it. I put a single quote mark on either side and used json.loads to turn it into a python dictionary.

loaded_body = json.loads(a_json)

And I am calling the only key from the beginning "some_body" which will get the contents of the entire body. When I run the following command print(loaded_body) I get back the error:

expecting ',' delimiter: line 1 column 18 (char 17)

Ultimately once that error is resolved, this is what I am trying to access:

desired_item =loaded_body['oItems'][0]['rAmount']['aPa']['currId']

Thanks

CodePudding user response:

It seems that you're treating the content of some_body as a string since it's enclosed with double quotes. But inside of that string there's also quotation marks and now it's interpreted that the content of some_body is [{ and then it breaks because directly after that is someId rather than a comma. Thus the error:

expecting ',' delimiter: line 1 column 18 (char 17)

If the content of some_body was actually meant to be a string then all the double quotes inside of it should be preceded by a double backslash (\\) although in this case you'd have to parse the JSON twice - first the entire a_json string and then the content of some_body. However I think it would be easier to just remove the double quotes around the content of some_body.

a_json = '{"some_body":
[{"someId":"189353945391","EId":"09358039485","someUID":10,"LegalId":"T743","cDate":"202452","rAmount":{"aPa":{"am":1500,"currId":"UD"},"cost":{"amount":1000,"currId":"US"},"lPrice":{"amount":100,"currId":"DD"}},"tes":{"ant":0,"currId":"US"},"toount":{"amnt":0,"currId":"US"},"toount":{"amt":210,"currId":"US"},"bry":"US","pay":[{"pId":"7111","axt":{"amt":2000,"currId":"US"},"mKey":"CSD"}],"oItems":[{"iIndex":0,"rId":"69823","provId":"001","segEntityId":"C001","per":{"vae":1,"ut":"MOS"},"pct":{"prod":"748"},"revType":"REW","rAmount":{"aPaid":{"amt":90000,"currId":"US"},"xt":{"amt":0,"currId":"USD"},"lPrice":{"amt":90000,"currId":"US"}},"stion":{"sLocal":"094u5304","eLocal":"3459340"},"tx":{"adt":{"adet":0,"currId":"US"},"era":"werTIC"
}}]}]}'

Also note that there's a missing closing square bracket near the end.

In your original string:

"werTIC"}}}]"}

Which should probably be:

"werTIC"}}]}]}

Now the desired item should look like this (I assume that 'aPa' was meant to be 'aPaid'):

desired_item = loaded_body['some_body'][0]['oItems'][0]['rAmount']['aPaid']['currId']
print(desired_item)
US
  • Related