0 votes
1 view
in Python by (40.4k points)

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)

I'm expecting json1_data to be a dict type but it actually comes out as a list type when I check it with type(json1_data).

What am I missing? I need this to be a dictionary so I can access one of the keys.

1 Answer

0 votes
by (92.9k points)

To convert JSON string to Dictionary you can use the below-mentioned code:-

import json 

with open('<yourFile>.json', 'r') as JSON: 

json_dict = json.load(JSON) 

print(json_dict["username"])

Related questions

0 votes
2 answers
asked Aug 3 in Python by Sammy (40.4k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...