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 OverflowYour 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]
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):
Copy# 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"))
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 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())
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]
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):
Copy# 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"))
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.
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
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]
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.
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!
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.
Solving the particular problem I am having right now.
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: 0Regarding 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.