0

I have the following json which I am trying to parse into a dictionary so that I get just the queries in the "Queries" that is query 1 and query 2

  [ 
      { "A":  "xyz", 
        "B": "this is xyz",
        "Queries":    [
                       "Query 1",
                       "Query 2" 
                      ] 
      }
    ]

I am using:

import json 
js=open('C://localpath//files.json') 
data=json.load(js) //assuming that json.load() will make data as dictionary correct me if i am wrong

data.get("Queries") ives the following error

Traceback (most recent call last): File "", line 1, in AttributeError: 'list' object has no attribute 'get'

How do I get data to be a dictionary and not a list and use "Queries" to just get the queries out of json, I don't want to convert it to a list. Is there a way to directly treat it as a dictionary?

2 Answers 2

2

try the following

js = open('C:/temp/json.txt').read()
data = json.loads(js)
data[0]['A']
u'xyz'
Sign up to request clarification or add additional context in comments.

Comments

1

data is a list of dictionaries. You would need to index the dictionary out of the list

data[0].get('Queries')

Or if there are multiple dictionaries, you could use a list comprehension to get the queries from each dictionary

[d.get('Queries') for d in data]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.