Convert json to Dictionary

Asked

Viewed 359 times

-1

I’m trying to convert json the data into a dictionary. This json_test to be a Dict type but actually comes as a list type when I check with type(json1_data).

import json
json1_file = open('json_teste')
json1_str = json1_file.read()
json1_data = json.loads(json_str)
  • 1

    A tip: if you opened the file, you don’t need to play all the content in a string to then read the JSON, you can do it at once: json1_data = json.load(json1_file)

  • Thanks for the tip.

1 answer

0


Your JSON is probably an array with a single or multiple objects inside, so when you read it, you get a list with a dictionary inside. You can access your dictionary by accessing the value json.loads(json1_str)[0] on the list.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.