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

Copydata = json.load(f)[0]
Answer from Александр Свито on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-json-to-dictionary-in-python
Convert JSON to dictionary in Python - GeeksforGeeks
July 12, 2025 - In the below code, firstly we open the "data.json" file using file handling in Python and then convert the file to Python object using the json.load() method we have also print the type of data after conversion and print the dictionary.
🌐
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 - Parse JSON Data: Use the json.loads() function to parse the JSON data from the file into a Python dictionary. ... Here, parsed_data will be a dictionary containing the contents of the JSON file.
🌐
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!

🌐
Index.dev
index.dev › blog › convert-json-to-dictionary-python
How to Convert JSON to a Python Dictionary: Step-by-Step Guide
As we can see, the JSON text is successfully translated into a Python dictionary with the identical structure. In addition to importing JSON strings, you may need to import JSON data from files. Python's json.load() function is suitable for this purpose.
🌐
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
The with statement is a python control-flow structure that simplifies the process of reading and closing the file. Note that we use the load method instead of loads because this is a file. Let's see what the loaded data looks like in Python: Note that the JSON object is loaded as dictionary in ...
🌐
freeCodeCamp
freecodecamp.org › news › python-read-json-file-how-to-load-json-from-a-file-and-parse-dumps
Python Read JSON File – How to Load JSON from a File and Parse Dumps
October 27, 2020 - This is a variable that we can use within the with statement to refer to the file object. ... json.load(file) creates and returns a new Python dictionary with the key-value pairs in the JSON file.
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())
Find elsewhere
🌐
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.

🌐
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 - So to use it in our application, we need to convert JSON string into a Python dictionary. Using the json.loads() method, we can deserialize native String, byte, or bytearray instance containing a JSON document to a Python dictionary.
🌐
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.
🌐
AskPython
askpython.com › home › how to convert json to a dictionary in python?
How to convert JSON to a dictionary in Python? - AskPython
February 16, 2023 - <class 'dict'> Key: Linux Value: ... how to read a JSON file and then convert it into a Python dictionary using json.load() function....
🌐
Reddit
reddit.com › r/learnpython › what's the real difference between a dict and a json object (coming from js background)
r/learnpython on Reddit: What's the real difference between a dict and a JSON object (coming from JS background)
October 9, 2022 -

Hello all,

I'm going through 100 Days of Python and reading "Introducing Python" by O'Reilly one thing that bothers me is the dict (insert funny joke). But seriously, what's the difference between a dict and a JSON object in JavaScript? Is there really any difference or should I just treat them as the same?

Kind regards

Top answer
1 of 4
15
JSON is a file format that uses a key - value pair syntax. Keys can only be unicode strings surrounded by doubel quotes, like "name". Values can be: Only certain number formats like 5, -5, 5.5, 1.0E+5 The values true, false, and null Unicode strings in double quotes, like "hello" Array: an ordered, comma separated list of any valid value type Object: a collection of key-value pairs that itself confirms to the JSON syntax ---------------------------------- A Python dict is an object that can have any hashable object as a key. It’s more flexible. A number, a decimal, a string, a tuple, etc can be a key. A Python dict value has no limits (that I know of). It can be any Python object. A string, a number, a function, another dict, a whole entire module. You name it. ---------------------------------- What makes JSON so powerful is that it's an agreed upon format. No matter if you're using Python, Java, PHP, or Ruby, if someone sends your app data in the JSON format, the shape is predictable so your programming language of choice will be able to parse it. How Java, Python, JavaScript, etc. decide to parse JSON into an object will be a little different depending on the language. Python can decode a string or file that confirms to the JSON format into a Python dict. ---------------------------------- One nuance that I wanted to point out is even though JSON stands for "JavaScript Object Notation", it is NOT JavaScript. JSON is a file format/data exchange format. Someone said "So 1 and 1.0 are two different values in Python, while in JSON, they would be the same." That's not quite true. JSON is only a format. 1 and 1.0 in a json file are no more "the same" as 1 and 1.0 are in a txt file. What that person probably meant is that 1 and 1.0 are the same in JavaScript, but that's besides the point because it's important to remember that JSON is NOT JavaScript, it's a format.
2 of 4
4
The most obvious difference is that JSON objects have to have strings as keys, and JSON values as values (which can only be null, true, false, numbers, strings, arrays or objects). Python dictionaries can have any Python object as either a key or a value. Also they are just different languages and there are subtle differences in the way they model data even if they are roughly equivalent a lot of the time. For example, Python distinguishes ints from floats, whereas in JSON there are only "numbers". So 1 and 1.0 are two different values in Python, while in JSON, they would be the same.
🌐
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 - Let’s discuss how to convert the JSON string object to a Dictionary in python. From JSON string, we can convert it to a dictionary using the json.loads() method. Suppose you have a JSON file, then loads() will not work.
🌐
Oregon State University
blogs.oregonstate.edu › logicbot › 2022 › 04 › 08 › saving-a-dictionary-with-json
Saving a dictionary with JSON – Logic_bot
json.loads() turns JSON formatted data back into a dictionary that you can reference normally. There is a subtle difference in the prints but if you have double quotes around you dictionary when you print, it is likely still in JSON format as in the first line below.
Top answer
1 of 2
1

If you update your file to contain a single JSON object, you can access the dictionaries within it using the json.load() function.

with open("file.json") as json_file:
    items = json.load(json_file)

    dict1 = items[0]
2 of 2
0

You do not have a JSON file. You have a file that is a concatenation of multiple JSON documents. json.load cannot handle this; you have to go lower level with JSONDecoder.raw_decode. This code will load your file into a list:

import json

with open('file.json', 'rt') as r:
    raw_json = r.read()

decoder = json.JSONDecoder()
items = []
while raw_json:
    item, pos = decoder.raw_decode(raw_json)
    raw_json = raw_json[pos:].strip()
    items.append(item)

from pprint import pprint
pprint(items)

# => [{'Amazon': {'email': '[email protected]', 'password': '123456'}},
#     {'Stack Overflow': {'email': '[email protected]',
#                         'password': 'password'}}]

(assuming the file doesn't actually have trailing commas before closing braces)

Of course, if you only wish to read the n-th record, you can stop reading after having read n records, instead of accumulating the results in a list.

Note that this is not a standard format. Not all programming languages allow you to JSON-parse a prefix of a string (e.g. in JavaScript you would either have to write a custom parser from scratch, or hack the error message to see where the error occured so you can cut the string off there — neither option is pretty). Use standard formats wherever possible. For example, JSONL (the same format but unindented, with one JSON document per line) is easily parseable in any language because you can predictably cut the raw string into lines before JSON parsing commences, while still being appendable, like your format.

🌐
Reddit
reddit.com › r/learnpython › json file to dictionary
r/learnpython on Reddit: json file to dictionary
April 21, 2021 -

Hi,

I'm trying to import json file and make a new dictionary of it. I have to swap the key and value when I'm making this as a new dict.

I've came up until here so far:

import json

j = open(file, "r")

f = json.load(j)

new = dict([(value, key) for key, value in f.items()])

it occurs error as unhashable type: "list"

Any suggestion to go further?

Thank you!

🌐
Reddit
reddit.com › r/learnpython › really struggling with parsing json and dictionaries
r/learnpython on Reddit: Really struggling with parsing json and dictionaries
June 23, 2022 -

So I do cloud devops and have managed to create a lot of automations using BASH. For example, using AWS cli tools with the default output format of json, I have written many scripts using the AWS cli where I pipe the output to jq and get the results I am looking for. Combined with tools like jqplay, I have accomplished a lot. But there is a limit to BASH's usefulness when you are doing more complex operations. For that reason I have tried to lean in and do more stuff in python. I have gotten pretty good at modifying existing code and have written some pretty useful smaller python scripts.

But several times over the last few months, I keep trying and failing to really comprehend pythons handling of json. Such that I have given up and gone back to bash to complete a project.

So I am asking for help with two things from r/learnpython.

  1. Solving the particular problem I am having right now.

  2. Finally understanding how to parse any json with python.

ONE - - - - - - My current problem.

So using the code below, I have learned how to just get my data using boto3, convert it to json using json.dumps() and pretty print the json. (By the way, I need to use the standard json library here)

import boto3, json
from sys import argv

account = argv[1]

##THE FUNCTION BELOW WORKS FINE AND IS NOT REALLY RELEVANT TO MY QUESTION
def get_app_vpc_name(account): 
    if 'sbx' in account:
        return 'sbx-app-' + account
    elif 'dev' in account:
        return 'dev-app-' + account
    elif 'tst' in account:
        return 'tst-app-' + account
    elif 'prd' in account:
        return 'prd-app-' + account

## SETUP boto3 FOR AWS API 
boto3.setup_default_session(profile_name=account)
ec2 = boto3.client('ec2')

## GET A PARTICULAR VPC OUTPUT
def get_app_vpc_cidr_block(account):
    app_vpc_cidr_blk_name = '-'.join([account, 'app-vpc-cidr-block'])
    vpc = ec2.describe_vpcs(
    Filters=[
        {
            'Name': 'tag:Name',
            'Values': [
                get_app_vpc_name(account)
            ]
        }
    ]
    )
## CONVERT PYTHON DICTIONARY TO JSON USING json.dumps
    vpc_json = json.dumps(vpc, indent=6)

## PRETTY PRINT THE JSON
    print(vpc_json)

---- output FROM ABOVE CODE

{
      "Vpcs": [
            {
                  "CidrBlock": "10.215.188.0/22",
                  "DhcpOptionsId": "dopt-a370e999",
                  "State": "available",
                  "VpcId": "vpc-046b1f660f8337999",
                  "OwnerId": "999092819999",
                  "InstanceTenancy": "default",
                  "CidrBlockAssociationSet": [
                        {
                              "AssociationId": "vpc-cidr-assoc-027d7fe136117b999",
                              "CidrBlock": "10.215.188.0/22",
                              "CidrBlockState": {
                                    "State": "associated"
                              }
                        }
                  ],
                  "IsDefault": false,
                  "Tags": [
                        {
                              "Key": "network_tier",
                              "Value": "app"
                        },
                        {
                              "Key": "network_name",
                              "Value": "llh-devapp"
                        },
                        {
                              "Key": "ingress_support",
                              "Value": "false"
                        },
                        {
                              "Key": "usage",
                              "Value": "central-network"
                        },
                        {
                              "Key": "network_environment",
                              "Value": "dev"
                        },
                        {
                              "Key": "Name",
                              "Value": "dev-app-llh-devapp"
                        },
                        {
                              "Key": "account_name",
                              "Value": "N/A"
                        }
                  ]
            }
      ],
      "ResponseMetadata": {
            "RequestId": "9619218c-be04-4e36-bc4c-e8c8411a7999",
            "HTTPStatusCode": 200,
            "HTTPHeaders": {
                  "x-amzn-requestid": "9619218c-be04-4e36-bc4c-e8c8411a7999",
                  "cache-control": "no-cache, no-store",
                  "strict-transport-security": "max-age=31536000; includeSubDomains",
                  "content-type": "text/xml;charset=UTF-8",
                  "content-length": "1966",
                  "date": "Fri, 30 Sep 2022 19:40:09 GMT",
                  "server": "AmazonEC2"
            },
            "RetryAttempts": 0
      }
}

So I have made a lot of progress, but where I have struggled for weeks is parsing the json (or even the dictionary before I convert it to json) to get the exact data I need. Over and over and over, I keep running into type and other errors, but I have not succeeded in just parsing and get just the data I need. from the json/dictionary.

In this particular case, all I want is to get the CidrBlock from the data. I have had partial success by working with the code below, appended to the above script. (I will just show the function with the extra code)

def get_app_vpc_cidr_block(account):
    app_vpc_cidr_blk_name = '-'.join([account, 'app-vpc-cidr-block'])
    vpc = ec2.describe_vpcs(
    Filters=[
        {
            'Name': 'tag:Name',
            'Values': [
                get_app_vpc_name(account)
            ]
        }
    ]
    )
    vpc_json = json.dumps(vpc, indent=6)
    print(vpc_json)
    ## EXTRA CODE. USING DICTIONARY ITEMS. JSON CODE ABOVE IS IRELEVANT
    for key, value in vpc.items():
        results = value
        print(results[0]['CidrBlock'])

I would like to add that I have tried a LOT of differnet things to simply retrieve the CidrBlock data. Depending on whether or not I dumped it to json, I have gotten so many errors. (Often type errors, but no real success.

Here is what the code above returns after the json print. (NOTE that it does return the CidrBlock before the error).

10.215.188.0/22
Traceback (most recent call last):
  File "./caller.py", line 6, in <module>
     get_app_vpc_cidr_block(aws_acct)
  File "/var/lib/jenkins/testscripts/getAppVpcCidrBlock.py", line 35, in get_app_vpc_cidr_block
print(results[0]['CidrBlock'])
KeyError: 0

Regarding this problem in particular, my only question is this.

What is the correct pythonic way to retrieve the damned CidrBlock value from the above dictionary/json and assign the value to a variable?

2----MORE GENERAL JSON QUESTIONS

  • Do I even need to convert output like above from a dictionary to json? (After retrieving the data using boto3, it is of the dictionary type.)

  • Once you have an object (json or dictionary), what is the proper way to parse it and get particular data from it. In my case I will almost always want to retrieve certain values from the data and assign variables to those values.

  • To illustrate the above question, suppose I wanted to retrive the following values from the json and assign each value to a variable

    • CidrBLock

    • VpcId

    • AssociationId (from CidrBlockAssociationSet)

    • Name Value (from tags)

  • Is there a python tool similar to jqplay that I can use to play with json to find the right python to get the data I want?

Thanks and I appreciate any help y'all can give me on this.

Top answer
1 of 4
6
I have a feeling you're overthinking this. When you access a value using jq, you look through each level and determine which element you need from the next level. Sometimes, the item is an array, so you access it via its index; sometimes, it's an object, so you access it via its dictionary. Really it's exactly the same with Python, except you would be more explicit with the lookups. In this case, assuming you do only have one Vpc as shown in that JSON, you don't need any loops at all; you can just access it directly: cidr_block = vpc['Vpcs'][0]['CidrBlock'] The outer data structure is a dict, so access it via keys; its Vpc key contains a list, so access that via index; the only item in the list is a dict, so access it via the key you want. The reason why your loop gets the cidr and then fails is because it looks through every key in the outer dictionary; the first one is Vpc, so it works, but the second one is ResponseMetadata whose value is a dict, not a list, so you get the error as you're trying to index it like a list. And no, you don't need to dump it back to JSON; I'm not sure why you think you need to. JSON is a string format, which you can use to send data to and from an API, but you would only ever interact with that data in Python format.
2 of 4
2
quick demo: >>> import json >>> >>> data = {"A": {"x": 1, "y": 2}, "B": {"x" : 7, "y": 8, "z": 9}} >>> print(data, type(data)) {'A': {'x': 1, 'y': 2}, 'B': {'x': 7, 'y': 8, 'z': 9}} >>> >>> # turn a dict into a JSON string >>> s = json.dumps(data, indent=4) >>> print(s) { "A": { "x": 1, "y": 2 }, "B": { "x": 7, "y": 8, "z": 9 } } >>> >>> # turn a JSON string into a python dict >>> d = json.loads(s) >>> print(d, type(d)) {'A': {'x': 1, 'y': 2}, 'B': {'x': 7, 'y': 8, 'z': 9}} >>> >>> # access some data >>> print(d) {'A': {'x': 1, 'y': 2}, 'B': {'x': 7, 'y': 8, 'z': 9}} >>> print(d['A']) {'x': 1, 'y': 2} >>> print(d['A']['y']) 2 >>> print(d['B']['y']) 8 If you follow the basics here ^ then you should be able to do whatever you need to do... these are just nested (hierarchical) dicts and a notation (json) to express them as strings