Each Answer to this Q is separated by one/two green lines.
I am trying to pass in a JSON file and convert the data into a dictionary.
So far, this is what I have done:
import json json1_file = open('json1') json1_str = json1_file.read() json1_data = json.loads(json1_str)
json1_data to be a
dict type but it actually comes out as a
list type when I check it with
What am I missing? I need this to be a dictionary so I can access one of the keys.
Your JSON is an array with a single object inside, so when you read it in you get a list with a dictionary inside. You can access your dictionary by accessing item 0 in the list, as shown below:
json1_data = json.loads(json1_str)
Now you can access the data stored in datapoints just as you were expecting:
datapoints = json1_data['datapoints']
I have one more question if anyone can bite: I am trying to take the average of the first elements in these datapoints(i.e. datapoints). Just to list them, I tried doing datapoints[0:5] but all I get is the first datapoint with both elements as opposed to wanting to get the first 5 datapoints containing only the first element. Is there a way to do this?
datapoints[0:5] doesn’t do what you’re expecting.
datapoints[0:5] returns a new list slice containing just the first 5 elements, and then adding
 on the end of it will take just the first element from that resulting list slice. What you need to use to get the result you want is a list comprehension:
[p for p in datapoints[0:5]]
Here’s a simple way to calculate the mean:
sum(p for p in datapoints[0:5])/5. # Result is 35.8
If you’re willing to install NumPy, then it’s even easier:
import numpy json1_file = open('json1') json1_str = json1_file.read() json1_data = json.loads(json1_str) datapoints = numpy.array(json1_data['datapoints']) avg = datapoints[0:5,0].mean() # avg is now 35.8
, operator with the slicing syntax for NumPy’s arrays has the behavior you were originally expecting with the list slices.
Here is a simple snippet that read’s in a
json text file from a dictionary. Note that your json file must follow the json standard, so it has to have
" double quotes rather then
' single quotes.
Your JSON dump.txt File:
import json with open('/your/path/to/a/dict/dump.txt') as handle: dictdump = json.loads(handle.read())
You can use the following:
import json with open('<yourFile>.json', 'r') as JSON: json_dict = json.load(JSON) # Now you can use it like dictionary # For example: print(json_dict["username"])
The best way to Load JSON Data into Dictionary is You can user the inbuilt json loader.
Below is the sample snippet that can be used.
import json f = open("data.json") data = json.load(f)) f.close() type(data) print(data[<keyFromTheJsonFile>])
I am working with a Python code for a REST API, so this is for those who are working on similar projects.
I extract data from an URL using a POST request and the raw output is JSON. For some reason the output is already a dictionary, not a list, and I’m able to refer to the nested dictionary keys right away, like this:
datapoint_1 = json1_data['datapoints']['datapoint_1']
where datapoint_1 is inside the datapoints dictionary.