Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print particular JSON value in Python?

Tags:

python

json

So I have a Python code which returns a JSON string like this one:

'{"X": "value1", "Y": "value2", "Z": [{"A": "value3", "B": "value4"}]}'

What I want to do is to print and/or return (in Python) "value 3" in order to use it. Also assign it to a variable so I can work with it later on.

How can I do this?

like image 465
Jmlevick Avatar asked Feb 01 '12 09:02

Jmlevick


1 Answers

>>> import json
>>> a = json.loads('{"X":"value1","Y":"value2","Z":[{"A":"value3","B":"value4"}]}')
>>> a
{'Y': 'value2', 'X': 'value1', 'Z': [{'A': 'value3', 'B': 'value4'}]}
>>> a["Z"][0]["A"]
'value3'
like image 80
Tim Pietzcker Avatar answered Oct 25 '22 08:10

Tim Pietzcker