2021年4月3日星期六

Json needs to be parsed twice?

I have json string that I have collected from a website that has a formatting with escape characters:

Code when trying to parse:

collected = "{\u0022id\u0022:1990,\u0022media_id\u0022:\u00225299\u0022}  

json_dict = json.loads(collected)

Output:

{"id":1990,"media_id":"5299"}  *This does not work*  

When I try to get the value of a key it returns an error: json_dict["id"] , TypeError: string indices must be integers

However I had recently discovered a temporary solution that solves this problem in which I have to parse it twice using the json.loads:

Code for the solution:

json_dict = json.loads(json.loads(collected))

Output:

{'id' : 1990, 'media_id' : '5299'}  *This works the best*  

And this actually acts like a dictionary instead of a string object

Now for my question Is there actually no other better way than parsing it twice? Or is there a better more pythonic method?

I have several hypothesis that there is a certain function that parses an escape character \u0022Text\u0022 to "Text" without using json.loads() so I would love to be informed if there is one.

https://stackoverflow.com/questions/66937581/json-needs-to-be-parsed-twice April 04, 2021 at 10:41AM

没有评论:

发表评论