0

I am new to JSON in general and having no idea why this fails. My thoughts where that is has something to do with the double quotes.

This is the JSON String i want to load (printed with print(resultJson)):

"{"Paging":{"PageSize":20,"PageIndex":1,"TotalRecords":1},"Result":{"VerifyResult":1,"Data":[{"Id":"a2b6a53eb992b6a9e682f81450b39ce3","RegGov":"龙湖海关","TradeType":"进出口收发货人","RegDate":"2002-08-30"}]},"Status":"200","Message":"查询成功","OrderNumber":"..."}"

This is my code to do it:

# Get the Results:
print(response.status_code)
resultJson = json.dumps(str(response.content, encoding = encode))
# convert unicode to chinese
resultJson = resultJson.encode(encode).decode("unicode-escape")

print(resultJson)

print(json.loads(resultJson)["Result"])

This is the Exception:

  File "C:\[...]\lib\json\decoder.py", line 340, in decode
    raise JSONDecodeError("Extra data", s, end)

JSONDecodeError: Extra data

What do i need to change? I am i converting/decoding something wrong?

K-Doe
  • 509
  • 8
  • 29
  • Can you print the resultJson before encoding, and show the output here? – Ishwar Venugopal Jan 31 '21 at 11:49
  • Does this answer your question? [Python json.loads shows ValueError: Extra data](https://stackoverflow.com/questions/21058935/python-json-loads-shows-valueerror-extra-data) – Tugay Jan 31 '21 at 12:29
  • 1
    If your string actually begins and ends with `"`, get rid of these characters. – Booboo Jan 31 '21 at 13:10
  • Thanks @Booboo, this is working fine. Thought maybe something else was wrong with my stuff. Thank you! – K-Doe Jan 31 '21 at 14:43
  • By the way, the expression `str(response.content, encoding = encode)` is taking `response.content`, whatever it may be` and converting into a string and so when you call `json.dumps` passing it as an argument, the result, `resultJson`, will have `"` characters at the beginning and end because the JSON representation of a `str` object has those characters. Maybe you just want: `resultJson = str(json.dumps(response.content, ensure_ascii=False), encoding=encode)`. Although perhaps just: `resultJson = json.dumps(response.content, ensure_ascii=False)` and you are done. – Booboo Jan 31 '21 at 15:29
  • I should add that if `response.content` is already a JSON string representations of a `dict`, then I do not know why your are calling `json.dumps` on it. – Booboo Jan 31 '21 at 15:36

0 Answers0