Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Python
Posted by: Cammy And
Added: Jan 27, 2021 11:10 AM
Views: 4
Tags: no tags
  1. def json_extract(obj, key):
  2.     """Recursively fetch values from nested JSON."""
  3.     arr = []
  4.  
  5.     def extract(obj, arr, key):
  6.         """Recursively search for values of key in JSON tree."""
  7.         if isinstance(obj, dict):
  8.             for k, v in obj.items():
  9.                 if isinstance(v, (dict, list)):
  10.                     extract(v, arr, key)
  11.                 elif k == key:
  12.                     arr.append(v)
  13.         elif isinstance(obj, list):
  14.             for item in obj:
  15.                 extract(item, arr, key)
  16.         return arr
  17.  
  18.     values = extract(obj, arr, key)
  19.     return values