Aug-15-2022, 08:33 PM
(This post was last modified: Aug-15-2022, 08:49 PM by deanhystad.)
How a json file starts depends on what you write to the file.
json.dump([1, 2, 3, 4], file) writes a file that contains [1, 2, 3, 4].
json.dump({"one":1, "two":2, "three":3}, file) writes a file that contains {"one": 1, "two": 2, "three": 3}.
json.dump("hello", file) writes a file that contains "hello".
json.dump(42, file) writes a file that contains 42.
Look at my earlier post. It shows how to drill down and look at the object created by json.load() or json.loads().
Try running this code. Maybe it will help you understand.
json.dump([1, 2, 3, 4], file) writes a file that contains [1, 2, 3, 4].
json.dump({"one":1, "two":2, "three":3}, file) writes a file that contains {"one": 1, "two": 2, "three": 3}.
json.dump("hello", file) writes a file that contains "hello".
json.dump(42, file) writes a file that contains 42.
Look at my earlier post. It shows how to drill down and look at the object created by json.load() or json.loads().
Try running this code. Maybe it will help you understand.
import json # Replace with name of your json file with open("data.txt", "r") as f: data = json.load(f) # data is a list, because the json file starts with "[" and ends with "]". # We can index a list or loop through a list. Here we loop. for thing in data: # thing is a dictionary. You can tell this because it starts with "{" and ends with "}". # It has keys "kind" and "entries". print(thing["kind"]) for entry in thing["entries"]: # thing["entries"] is a list. for key in entry: # entry is a dictionary. Here we loop through the dictionary keys which we use # to pretty print the dictionary values. print(f" {key.title():20} {entry[key]}") print() print()