0

https://hackersandslackers.com/extract-data-from-complex-json-python/-> took the below python code from here

I really need a java code to pull all values of user input specified key from nested json. It takes json as input with key name and returns a list of all values for that particular key in a nested json. i have trouble convering as traversing through json node using java is more complex. Anyone has similar solution ?

def extract_values(obj, key):
    """Pull all values of specified key from nested JSON."""
    arr = []

    def extract(obj, arr, key):
        """Recursively search for values of key in JSON tree."""
        if isinstance(obj, dict):
            for k, v in obj.items():
                if isinstance(v, (dict, list)):
                    extract(v, arr, key)
                elif k == key:
                    arr.append(v)
        elif isinstance(obj, list):
            for item in obj:
                extract(item, arr, key)
        return arr

    results = extract(obj, arr, key)
    return 
Abhi Sonu
  • 9
  • 1
  • Does this answer your question? [How to parse JSON in Java](https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – donquih0te Feb 27 '20 at 06:57
  • No Diinar, as i want to know how to do for nested json. i found how to get keys from all the json nested data but i want to check the json with the key passed as user inpout and output all the values of it in list – Abhi Sonu Feb 27 '20 at 07:01
  • Is use of libraries such as Gson or Jackson not allowed? – Sanjay Bharathi Feb 27 '20 at 11:19

0 Answers0