5

I have a JSON string from which I am trying to extract a property value using Python as shown below:

def extract_property(node, to_extract):
    data, stat = zk.get(node)
    jsonString = data.decode("utf-8")
    jStr = json.loads(jsonString)
    return jStr[to_extract]

Now it is possible, the property value that I am trying to extract doesn't exist in that JSON string so it will fail. How can I return empty string if property doesn't exist at all in the JSON string.

This line can fail if property doesn't exist.

return jStr[to_extract]
john
  • 11,311
  • 40
  • 131
  • 251
  • `if to_extract in jStr:` – Barmar Oct 11 '16 at 19:59
  • Couldn't you just go `if jStr[to_extract]: return jStr[to_extract] else: return None` – MooingRawr Oct 11 '16 at 19:59
  • @MooingRawr That will get the same error if the property doesn't exist. – Barmar Oct 11 '16 at 20:00
  • @barmar I see, learn something new everyday, For some reason I thought JSON objects are special and would return nothing if they couldn't find the property. Thinking about it now, that seems rather silly. – MooingRawr Oct 11 '16 at 20:04
  • @MooingRawr There's no such thing as a JSON object. JSON is a string representation of data structures. After you decode it, it's just a native data type. In this case, it's a Python dictionary. – Barmar Oct 11 '16 at 20:07
  • Another related and possible dupe http://stackoverflow.com/questions/24898797/check-if-key-exists-and-iterate-the-json-array-using-python – Bhargav Rao Oct 11 '16 at 20:08
  • @Barmar **Not** a duplicate of any `dict`-specific question, for reasons made clear in the OP-accepted answer. – jez Oct 12 '16 at 01:47
  • @jez Since he's trying to access a specific property as if it's a `dict`, I think the assumption that the JSON is known a priori to be an object is reasonable. It's only the property name that's dynamic. – Barmar Oct 12 '16 at 15:54
  • @Barmar same syntax could be for accessing a sequence element, rather than a dict element, and the fact that the OP said it "didn't work for me" ***until*** this was taken into account in the answer, indicates that this was in fact the problem. – jez Oct 12 '16 at 15:58

2 Answers2

3

Simply use dict.get(), i.e.:

return jStr.get(to_extract, '')

See https://docs.python.org/3/library/stdtypes.html#dict.get for more details.

UPD:

Thanks to @jez for pointing out, that jStr is not guaranteed to be a dictionary. However, the result for JSON parsing is known: if it's not a dictionary, then it's a list, number or a string. In this case, wrap it into a type checking routine, e.g.:

try:
    return jStr[to_extract]
except (KeyError, AttributeError):
    return ''
Zaur Nasibov
  • 22,280
  • 12
  • 56
  • 83
1

Like Zaur, I would also have suggested jStr.get(to_extract, '') but I presume the OP's objection to this is that jStr might or might not be a dict (if it is a dict, then .get() will work in Python 2 or 3). If that's the problem, then the following might cover a broader range of cases:

try: return jStr[to_extract]
except: return ''
jez
  • 14,867
  • 5
  • 37
  • 64