15

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?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Jmlevick
  • 6,504
  • 9
  • 29
  • 35
  • 1
    Sorry for the mistake on the code!! thnx! – Jmlevick Feb 01 '12 at 09:57
  • NOTE: I'm using the "simplejson" module to parse the data – Jmlevick Feb 01 '12 at 10:01
  • Your input still is no valid JSON. `value1` and `value4` must be inside quotes. Even `simplejson` can't decode your input. –  Feb 01 '12 at 10:02
  • Please post some of the code you are trying so we don't have to guess which module you use. –  Feb 01 '12 at 10:02
  • 1
    Btw, simplejson is same as the json library. http://stackoverflow.com/questions/712791/json-and-simplejson-module-differences-in-python – amit kumar Feb 01 '12 at 10:15
  • But make sure you really need a list here: "Z":[{"A":"value3","B":"value4"}]. Maybe "Z":{"A":"value3","B":"value4"} will be enough, then you could get to value3 like this: a["Z"]["A"], instead of a["Z"][0]["A"]. – Anke Feb 01 '12 at 10:19

2 Answers2

23
>>> 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'
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • Oh! two Things: I'm using "simplejson" module to parse the data, and my string is like the recently edited one in my question... Does your answer applies? because I'm using simplejson.loads(datavariable) – Jmlevick Feb 01 '12 at 10:01
  • Thanks! This worked flawlessly for what I was trying to achieve! – Jmlevick Feb 01 '12 at 10:10
  • Any tutorial I can read in order to get more familiar with reading certain values from JSON strings in Python? – Jmlevick Feb 01 '12 at 10:21
  • 1
    Well, where was the problem? Was it loading the JSON string into a Python object, or was it finding the correct way to access elements? – Tim Pietzcker Feb 01 '12 at 10:28
  • The correct way to access the elements... I know how to parse json data in python and assign a variable for that "task" but what I wasn't sure is about to "pick" special values from that data and assign them a variable too... Any suggestions on reading material? :) – Jmlevick Feb 02 '12 at 07:16
  • That's very basic Python knowledge - I'd suggest the excellent [Python tutorial](http://docs.python.org/tutorial/index.html) on the official website (you should read it entirely anyway, but [here](http://docs.python.org/tutorial/introduction.html#lists), [here](http://docs.python.org/tutorial/datastructures.html#more-on-lists) and [here](http://docs.python.org/tutorial/datastructures.html#dictionaries) are the most relevant sections for this). – Tim Pietzcker Feb 02 '12 at 07:20
4

OK, I assume your JSON looks like this (note the " around each value):

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

Then you can do this:

import json
j = '{"X":"value1", "Y":"value2", "Z":[{"A":"value3", "B":"value4"}]}'
k = json.loads(j)
assert k["Z"][0]["A"] == "value3"

Edit: Even simplejsoncan't decode your original input.

>>> import simplejson
>>> s1 = '{"X":value1,"Y":"value2","Z":[{"A":"value3","B":value4}]}'
>>> simplejson.loads(s1)
simplejson.decoder.JSONDecodeError: No JSON object could be decoded: line 1 column 0 (char 0)
>>> s2 = '{"X":"value1", "Y":"value2", "Z":[{"A":"value3", "B":"value4"}]}'
>>> print simplejson.loads(s2)["Z"][0]["A"]
value3
  • Oh! two Things: I'm using "simplejson" module to parse the data, and my string is like the recently edited one in my question... Does your answer applies? because I'm using simplejson.loads(datavariable) – Jmlevick Feb 01 '12 at 10:00