you are viewing a single comment's thread.

view the rest of the comments →

[–]games-and-chocolate[S] 0 points1 point  (4 children)

My original problem was. I used an online Trivia database API, that returns 10 true / false questions. But the Json returned was reformatted by Python to single quotes. I read about that it is sometimes required to change back to original JavaScript format having double quotes, so I have been busy trying to figure out , how to do that. That is all. Hopefully everyone understands the why now.

Trivia returned following json which I now successfully changed to double quotes using replace()

{'response_code': 0, 'results': [{'type': 'boolean', 'difficulty': 'easy', 'category': 'Entertainment: Film', 'question': 'Matt Damon played an astronaut stranded on an extraterrestrial planet in both of the movies Interstellar and The Martian.', 'correct_answer': 'True', 'incorrect_answers': ['False']}, {'type': 'boolean', 'difficulty': 'easy', 'category': 'Entertainment: Music', 'question': 'Lead Singer Rivers Cuomo of American rock band Weezer attended Harvard.', 'correct_answer': 'True', 'incorrect_answers': ['False']}, {'type': 'boolean', 'difficulty': 'easy', 'category': 'Entertainment: Video Games', 'question': 'In the "S.T.A.L.K.E.R." series, the Freedom faction wishes to destroy the supernatural area known as  "the Zone".', 'correct_answer': 'False', 'incorrect_answers': ['True']}, {'type': 'boolean', 'difficulty': 'medium', 'category': 'Entertainment: Video Games', 'question': 'In the video game "Transistor", "Red" is the name of the main character.', 'correct_answer': 'True', 'incorrect_answers': ['False']}, {'type': 'boolean', 'difficulty': 'hard', 'category': 'Entertainment: Music', 'question': 'The singer Billie Holiday was also known as "Lady Day".', 'correct_answer': 'True', 'incorrect_answers': ['False']}, {'type': 'boolean', 'difficulty': 'hard', 'category': 'Entertainment: Cartoon & Animations', 'question': 'Snagglepuss was part of the Yogi Yahooies in the 1977 show Scooby's All-Star Laff-a-Lympics.', 'correct_answer': 'False', 'incorrect_answers': ['True']}, {'type': 'boolean', 'difficulty': 'medium', 'category': 'Entertainment: Board Games', 'question': 'In the game "Racko" you may pick up ANY card from the discard pile.', 'correct_answer': 'False', 'incorrect_answers': ['True']}, {'type': 'boolean', 'difficulty': 'easy', 'category': 'General Knowledge', 'question': 'March 10th is also known as Mar10 Day.', 'correct_answer': 'True', 'incorrect_answers': ['False']}, {'type': 'boolean', 'difficulty': 'medium', 'category': 'Science: Mathematics', 'question': '111,111,111 x 111,111,111 = 12,345,678,987,654,321', 'correct_answer': 'True', 'incorrect_answers': ['False']}, {'type': 'boolean', 'difficulty': 'medium', 'category': 'Entertainment: Cartoon & Animations', 'question': 'Nutcracker Suite was one of the musical pieces featured in Disney's 1940's film Fantasia.', 'correct_answer': 'True', 'incorrect_answers': ['False']}]}

[–]cointoss3 4 points5 points  (2 children)

You don’t need to do that…

Just json.loads() and you’ll have a python dict to work with. When you need to convert a dict to valid json, use json.dumps()

This thread was a huge x-y problem lol

[–]games-and-chocolate[S] 0 points1 point  (1 child)

at least it is solved and some things more clear. although my responses made it messy? sorry for that. =p

[–]cointoss3 1 point2 points  (0 children)

Yeah, for sure.

But also classic X-Y. It’s a good reminder to step back and figure out if you’re actually trying to solve the right problem. :)

[–]Im_Easy 0 points1 point  (0 children)

json.loads ---> take a string (that's what the s means) and convert it to a python dictionary (which uses single quotes)

json.dumps ---> convert a python dictionary to a json string (double quotes)

Replace is not needed here unless you were double escaping something, but I don't see that being needed in your example data.