3

I need help on how do I use python to access element from JSON structure.

Assuming I have a JSON like this

{
'result':
  [
    {
     'aa':1, 
     'bb':2
    },
    {
     'cc':3, 
     'dd':4
    }
  ]
 }

In python, how exactly to get the data for aa or dd ? I tried with

str1 = {'result':[{'aa':1, 'bb':2},{'cc':3, 'dd':4}]}
str1new = str1['result']['aa]

but it gives me error

list indices must be integers, not str

How do I solve this? Is there any other method using python to get the data from JSON? Thank you for the help. I really appreciated it.

7 Answers 7

6

Try this and for the next ones use indexes like 1 or 2 if you have more, or you can loop if you have multiple indexes within the json.

str1new = str1['result'][0]['aa']
Sign up to request clarification or add additional context in comments.

3 Comments

there is a typo there but never mind. can you explain the index [0] there? what does it means?
well the 0 is for the index as you do in list or dictionary , you can see the starting [] which means its a index start
@Exprator well the 0 is for the index as you do in list or dictionary? Dictionary is not indexed.
2

In python if you write:

str1 = {'result':[{'aa':1, 'bb':2},{'cc':3, 'dd':4}]}

it's dictionary and not json.

if your input is having json string you need to use

import json

json_str = """{
"result":
  [
    {
     "aa":1, 
     "bb":2
    },
    {
     "cc":3, 
     "dd":4
    }
  ]
 }"""
str1 = json.loads(json_str)

then you can use similar to python dictionary.

as answered by others you can then use

aa = str1['result'][0]['aa']

Comments

1

try

str1new = str1['result'][0]['aa]

Comments

1

As str1['result'] is a list, that's why you're getting list indices must be integers, not str error.

aa = str1['result'][0]['aa']
dd = str1['result'][1]['dd']

1 Comment

thank you. now I understand why it needs [0] or [1]in the answer based on your and others answer
1

result is a list, so you need to reference a valid index before accessing aa:

str1 = {'result':[{'aa':1, 'bb':2},{'cc':3, 'dd':4}]}
str1new = str1['result'][0]['aa']

Comments

1

In Python, unless you're using a package (then specify it), "JSON" structure are actually dicts.

{
'result':
  [
    {
     'aa':1, 
     'bb':2
    },
    {
     'cc':3, 
     'dd':4
    }
  ]
 }

Here, your dict has only one key : result. Its values are contained in a list. This list is a list of dicts. So use cascade index access to get your aa value :

str1['result'][0]['aa']

Comments

0

For Python3, using the Json library you can easily parse Json.

# import statement
import json

# using example sting from above
jsonString = '{ "result" : [{ "aa": 1,"bb": 2 }, { "cc": 3, "dd": 4 }]}'

# parse json string
parsedJsonObject = json.loads(jsonString)

# access a specific property in the json object 
resultObject = parsedJsonObject['result']

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.