Skip to content

Instantly share code, notes, and snippets.

@bhuiyanmobasshir94
Created April 18, 2020 00:41
Show Gist options
  • Save bhuiyanmobasshir94/c9d3ec783835bb83dfbf7f7fa47bd5da to your computer and use it in GitHub Desktop.
Save bhuiyanmobasshir94/c9d3ec783835bb83dfbf7f7fa47bd5da to your computer and use it in GitHub Desktop.
How to find a particular json value by key?
from __future__ import print_function
import json
def find_values(id, json_repr):
results = []
def _decode_dict(a_dict):
try:
results.append(a_dict[id])
except KeyError:
pass
return a_dict
json.loads(json_repr, object_hook=_decode_dict) # Return value ignored.
return results
json_repr = '{"P1": "ss", "Id": 1234, "P2": {"P1": "cccc"}, "P3": [{"P1": "aaa"}]}'
print(find_values('P1', json_repr))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment