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.

Answer from DaoWen on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-json-to-dictionary-in-python
Convert JSON to dictionary in Python - GeeksforGeeks
July 12, 2025 - Create a json string and store it in a variable 'json_string' after that we will convert the json string into dictionary by passing 'json_string' into json.loads() as argument and store the converted dictionary in 'json_dict'.
Top answer
1 of 6
365

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.

2 of 6
31

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())
Discussions

python - How can I import the first and only dict out of a top-level array in a json file? - Stack Overflow
By the way, you could do: data, = json.load(f). This will mean that if your outer list unexpectedly contains more than one element, then it will give an error rather than silently ignore everything after the first one. (This is multiple assignment syntax using a 1-element tuple.) 2020-10-08T18:34:05.21Z+00:00 ... 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... More on stackoverflow.com
🌐 stackoverflow.com
Correct way of loading JSON from file into a Python dictionary - Stack Overflow
Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... I’m Jody, the Chief Product and Technology Officer at Stack Overflow. Let’s... 17 Why is json.loads an order of magnitude faster than ast.literal_eval? 9 reading text file back into a dictionary ... More on stackoverflow.com
🌐 stackoverflow.com
Convert JSON array to Dictionary
Hmm minda sharing an anonymized version of the file? It is hard to follow. from what i read it seems like it is a list of dictionaries and you want to merge all the dictionaries right? So from functools import reduce j = json.load(...) combined = reduce(lambda x,y: {**x, **y}, j) Explanation: Reduce takes running pairs and runs the lambda which combines dictionaries. So essentially it takes a running sum of the dictionaries to combine them. Most people like looping, but this is readable to me. For loop: ... combined = j[0] for d in j[1:]: combined = {**combined, **d} Note: in py3.9+ you can do x|y instead of {**x,**y} More on reddit.com
🌐 r/learnpython
6
2
March 22, 2022
How to turn JSON into objects in python?
It's easier (and probably better) if you define the class first. Then, turn the json into a dict, then into an instance of that class. import json class Person: def __init__(self, name, job): self.name = name self.job = job person_json = '''{ "name":"Harry", "job":"Mechanic" }''' person_dict = json.loads(person_json) person = Person(**person_dict) Edit: Sorry I didn't answer your exact question. But maybe that was still helpful. There's also attrdict . Making a whole class from a dict is certainly possible, but not something most people would want to do. And when you say "yield", do you mean you want the Person definition as Python code, or the Person class object? More on reddit.com
🌐 r/learnpython
8
2
September 2, 2021
🌐
Reddit
reddit.com › r/learnpython › convert json list to dictionary
r/learnpython on Reddit: convert JSON list to dictionary
January 13, 2024 -

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!

🌐
Medium
medium.com › @jimitdoshi639 › parse-content-in-json-file-into-a-dictionary-in-python-a-fun-and-informative-guide-88c169561550
Parse content in JSON file into a dictionary in Python: A fun and informative guide | by Jimit Doshi | Medium
May 4, 2024 - Accessing the Data: Now you can access the data in the dictionary as you would with any other dictionary in Python. ... Replace 'key' with the specific key you want to access in your JSON data. Putting it all together, here’s a complete example: ... # Open the JSON file with open('data.json', 'r') as file: data = file.read()# Parse JSON data into a dictionary parsed_data = json.loads(data)# Accessing the data print(parsed_data['key'])
🌐
Index.dev
index.dev › blog › convert-json-to-dictionary-python
How to Convert JSON to a Python Dictionary: Step-by-Step Guide
The simplest approach to translate a JSON-formatted text into a Python dictionary is with json.loads().
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › json-loads-in-python
json.loads() in Python - GeeksforGeeks
Explanation: json.loads(s) parses the JSON string s and converts it into a Python dict.
Published   January 13, 2026
🌐
PYnative
pynative.com › home › python › json › python json parsing using json.load() and loads()
Python JSON Parsing using json.load() and loads()
May 14, 2021 - Understand use of json.loads() and load() to parse JSON. Read JSON encoded data from a file or string and convert it into Python dict
🌐
W3Schools
w3schools.com › python › python_json.asp
Python JSON
Python has a built-in package called json, which can be used to work with JSON data. ... If you have a JSON string, you can parse it by using the json.loads() method. The result will be a Python dictionary.
🌐
Python Examples
pythonexamples.org › python-json-to-dict
Python JSON to Dictionary
To convert Python JSON string to Dictionary, use json.loads() function.
🌐
iO Flood
ioflood.com › blog › json-loads
json.loads() Python Function | Convert JSON to dict
February 1, 2024 - In this example, we have a JSON string json_string that represents a person named John who is 30 years old. The json.loads function is used to convert this JSON string into a Python dictionary, which is then printed out.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › convert json to dictionary in python
Convert JSON to Dictionary in Python - Spark By {Examples}
May 31, 2024 - The json.loads() function parses the JSON string and converts it into a Python dictionary (python_dict).
🌐
Python
docs.python.org › 3 › library › json.html
JSON encoder and decoder — Python 3.14.3 documentation
3 weeks ago - cls (a JSONDecoder subclass) – If set, a custom JSON decoder. Additional keyword arguments to load() will be passed to the constructor of cls. If None (the default), JSONDecoder is used. object_hook (callable | None) – If set, a function that is called with the result of any JSON object literal decoded (a dict).
🌐
NetworkAcademy
networkacademy.io › learning path: ccna automation (200-901) ccnaauto › data formats and data models › parsing json with python
Parsing JSON with Python | NetworkAcademy.IO
There are a few python methods used to load json data: load(): This method loads data from a JSON file into a python dictionary. loads(): This method loads data from a JSON variable into a python dictionary. dump(): This method loads data from the python dictionary to the JSON file.
🌐
PythonForBeginners.com
pythonforbeginners.com › home › load json into a python dictionary
Load JSON into a Python Dictionary - PythonForBeginners.com
February 8, 2023 - You can observe that the JSON format is almost similar to the format of a python dictionary. To load a json string into a python dictionary, you can use the loads() method defined in the json module.
🌐
Reddit
reddit.com › r/learnpython › convert json array to dictionary
r/learnpython on Reddit: Convert JSON array to Dictionary
March 22, 2022 -

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.

🌐
MLJAR
mljar.com › docs › python-read-json-from-file
Read JSON file to dict in Python
Load JSON file to dict. Print dict object with indentation. It is a good practice to check if file exists before reading it. There is Check if file exists recipe available. Code recipes from Python cookbook.
🌐
Segmentation-faults
segmentation-faults.github.io › python-json-to-dict
A Practical Guide to Loading JSON Files as Dictionaries in Python | Segmentation Faults’ Blog
November 9, 2024 - Load the data: Use json.load(file) to convert JSON content into a dictionary. Print data: print(data) displays the structure of the loaded dictionary.
🌐
Real Python
realpython.com › python-json
Working With JSON Data in Python – Real Python
August 20, 2025 - The argument for the load() function must be either a text file or a binary file. The Python object that you get from json.load() depends on the top-level data type of your JSON file.