Created
April 18, 2020 00:41
-
-
Save bhuiyanmobasshir94/c9d3ec783835bb83dfbf7f7fa47bd5da to your computer and use it in GitHub Desktop.
How to find a particular json value by key?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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