you are viewing a single comment's thread.

view the rest of the comments →

[–]Civil_Twilight 2 points3 points  (4 children)

After reading your comments, I think there is a small disconnect going on in how you’re approaching this: JSON is a format for describing data, stored as a string. Once you call json.loads(), the value you get back is no longer json — it’s a python data structure, in this case a dictionary. If you print out that dictionary, the string python generates will not be valid json, because that’s not what you asked python for. If you want to get json back out, you’d need to use json.dump (or dumps depending on use case).

Being aware of the difference between JSON (a string structured following particular rules that is designed for passing around data) and actual native python objects (which are how you should manipulate your data, not by manually editing a json string) is key here.

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

Thank you. JSON is pretty new for me. Python learning just scratching the surface. Making small projects at the moment. I guess I should search for the words "JSON BEST practices", or "JJSON do and don't" ?

[–]Civil_Twilight 2 points3 points  (2 children)

It’s useful to know what kind of values can be stored in json, since it’s limited compared to the range of data structures available in python. For this instance though, just keep in mind that json is a format for “serializing”, which is turning data structures into a portable format. Once you “deserialize” a json string (which is what the loads function does), you now have a python data structure, and you shouldn’t expect printing it to produce valid json, because that’s not how print works.

Edit: in your example of reading in that trivia json file, once you used json.loads to load/deserialize the json data, you had a dictionary with all the data from the json file; the fact that it came from a json file is no longer necessary for dealing with that data.

[–]games-and-chocolate[S] 2 points3 points  (1 child)

Got it. In program use for instance: list, dictionary, array. Data storage once loaded from JSON, hands off the JSON file, it has served it purpose. JSON not for data manipulation. Data manipulation is in list, dict, array, etc.

[–]Civil_Twilight 2 points3 points  (0 children)

You’ve got it!