Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k 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 (106k 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, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...