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)[0]
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[0][0]). Just to list them, I tried doing datapoints[0:5][0] 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][0] doesn't do what you're expecting. datapoints[0:5] returns a new list slice containing just the first 5 elements, and then adding [0] 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[0] for p in datapoints[0:5]]
Here's a simple way to calculate the mean:
sum(p[0] 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)[0]
datapoints = numpy.array(json1_data['datapoints'])
avg = datapoints[0:5,0].mean()
# avg is now 35.8
Using the , operator with the slicing syntax for NumPy's arrays has the behavior you were originally expecting with the list slices.
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)[0]
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[0][0]). Just to list them, I tried doing datapoints[0:5][0] 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][0] doesn't do what you're expecting. datapoints[0:5] returns a new list slice containing just the first 5 elements, and then adding [0] 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[0] for p in datapoints[0:5]]
Here's a simple way to calculate the mean:
sum(p[0] 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)[0]
datapoints = numpy.array(json1_data['datapoints'])
avg = datapoints[0:5,0].mean()
# avg is now 35.8
Using the , 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:
{"test":"1", "test2":123}
Python Script:
import json
with open('/your/path/to/a/dict/dump.txt') as handle:
dictdump = json.loads(handle.read())
Videos
I must first preface this with the fact that I’m extremely new to python. Like just started learning it a little over a week ago.
I have been racking my brain over how to convert a json object I opened and loaded into a dictionary from a list so I can use the get() function nested within a for loop to do a student ID comparison from another json file (key name in that file is just ID).
Below is the command I’m trying to load the json file:
With open(‘file.json’) as x: object=json.load(x)
When I print(type(object)), it shows up as class list.
Here’s a sample of what the json looks like:
[
{
“Name”: “Steel”,
“StudentID”: 3458274
“Tuition”: 24.99
},
{
“Name”: “Joe”,
“StudentID”: 5927592
“Tuition”: 14.99
}
]
HELP! Thank you!
Your data get's imported as list, because in your JSON file the main structure is an Array (squared brackets), which is comparable to a list in Python.
If you want just inner dict you can do
data = json.load(f)[0]
The accepted answer is correct, but for completeness I wanted to offer an example that is increasingly popular for people like me who search for "read json file into dict" and find this answer first (if just for my own reference):
# Open json file and read into dict
with open('/path/to/file.json') as f:
data = json.load(f)
# In the author's example, data will be a list. To get the dict, simply:
data = data[0]
# Then addressing the dict is simple enough
# To print "icon.svg":
print(data.get("image"))
Your data must have already been a JSON string to begin with and then you double-encoded it during
the json.dump. Then of course you need to double-decode it later. So instead of encoding the original JSON with JSON again, just write it to the file as-is:
with open(filename, 'w+') as f:
f.write(data)
Disclaimer: I don't know if this is the definite correct way, but it works for me:
jd0 = {
'foo': 1337,
'bar': 'baz'
}
# Dump it somewhere
with open('/Dump/it/somewhere/test.json', 'w') as fh:
json.dump(jd0, fh)
If I then load it, its a dict again:
with open('/Dump/it/somewhere/test.json', 'r') as fh:
jd1 = json.load(fh)
print type(jd1) == dict
Prints
True
I have a JSON array file that I want to convert to a dictionary. The file has only one pair of square brackets [] with a dictionary of sub dictionaries inside it. print(len(dict)) returns 14. I want to simply convert the file to a dict but using json.loads() creates a list and using json.loads(filename)[0] to get that sole large item of nested dictionaries inside the json array only returns the first dictionary object and not the entire 14.
I want to know if there’s another way of doing this besides a dictionary comprehension which I found, but don’t necessarily understand. Thanks.