Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
556 views
in Technique[技术] by (71.8m points)

python - How to replace quotation marks within JSON dictionary value?

I'm using Open Trivia DB's API to generate trivia questions.

import requests, json, urllib.parse
import 

url = "https://opentdb.com/api.php"

querystring = {"amount":"5","type":"multiple","encode":"url3986"}

response = requests.request("GET", url, params=querystring)
response_decoded = urllib.parse.unquote(response.text)
print(response_decoded)
response_dict = json.loads(response_decoded)
print(response_dict["results"][0])

However, I keep on running into an error one some occurrences, the error being:

Exception has occurred: JSONDecodeError
Expecting ',' delimiter: line 1 column 347 (char 346)

I've worked out that the error is because in some questions similar to Who is the main character in "gamename", there are quotation marks around gamename. In the context of the JSON I was returned it looks like this:

"question":"Who is the main character in "gamename"?",
"correct_answer":"maincharacter",
"incorrect_answers":["wrongname1","wrongname2","wrongname3"]

and the quotation marks around the gamename is messing the dictionary up.

Is there a way for me to replace the inner quotation marks only (around the gamename) to singular quotation marks, ', so that it doesn't mess up the dictionary structure?

question from:https://stackoverflow.com/questions/65895349/how-to-replace-quotation-marks-within-json-dictionary-value

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Don't parse the response yourself. Use requests for that and it will handle the quotes:

import requests

resp = requests.get(
    "https://opentdb.com/api.php",
    params={"amount": "5", "type": "multiple", "encode": "url3986"},
)
resp.raise_for_status()  # raise an Exception if the HTTP response failed
data = resp.json()  # Parse JSON from the response
print(data["results"][0])  # print it

Looking at the data, some of the inner fields appear URL-encoded. Use urllib.parse.unquote for these:

import urllib.parse

print(urllib.parse.unquote("Entertainment%3A%20Music"))  # Entertainment: Music

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...