Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving all the keys in a nested json in java

Tags:

java

json

This is the program i wrote:

    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.StringTokenizer;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

/**
 *
 * @author 311001
 */
public class NewClass {

    public static void main(String args[]) {
        JSONObject parentData = new JSONObject();
        JSONObject childData = new JSONObject();
        try {

            parentData.put("command", "login");
            parentData.put("uid", "123123123");
            childData.put("uid", "007");
            childData.put("username", "sup");
            childData.put("password", "bros");
            parentData.put("params", childData);
            System.out.println(parentData);

            Map<String, String> map = new HashMap<>();
            Iterator<?> iter = parentData.keys();
            while (iter.hasNext()) {
                String key = (String) iter.next();
                String value = parentData.getString(key);
                map.put(key, value);
            }

            for (Entry<String, String> entry : map.entrySet()) {
                System.out.println("key > " + entry.getKey() + "  : value = " + entry.getValue());
            }

            String testData = map.get("params.uid");
            System.out.println(testData);
            System.out.println("Tokenizing json");
            String resultStr = parentData.toString();
            System.out.println("String tokens ");
            StringTokenizer st = new StringTokenizer(resultStr);
            System.out.println(st.countTokens());
            while (st.hasMoreTokens()) {
                System.out.println(st.nextToken());
            }
            String testDat="abc :: result";
            StringTokenizer simpleString = new StringTokenizer(testDat);
            System.out.println("Tokenizing simple string");
            System.out.println(simpleString.countTokens());
            while (simpleString.hasMoreTokens()) {
                System.out.println(simpleString.nextToken());
            }


        } catch (JSONException e) {
            e.printStackTrace();
        }


    }
}

the output i got:

run:
{"command":"login","uid":"123123123","params":{"uid":"007","username":"sup","password":"bros"}}
key > uid  : value = 123123123
key > command  : value = login
key > params  : value = {"uid":"007","username":"sup","password":"bros"}
null
Tokenizing json
String tokens 
1
{"command":"login","uid":"123123123","params":{"uid":"007","username":"sup","password":"bros"}}
Tokenizing simple string
3
abc
::
result
BUILD SUCCESSFUL (total time: 0 seconds)

How can I recieve all the keys in my json object. In case I tokenize why do i get only one string token while for a simple string am getting the correct output 3 tokens.

like image 732
Aayush Avatar asked Feb 17 '23 16:02

Aayush


2 Answers

You can recursively traverse your JsonObject to get all keys. heres the pseudocode

findKeys(JsonObject obj,List keys){
List<String>keysFromObj=obj.keys();
keys.addAll(keysFromObj);
for(String key:keysFromObj){
    if(obj.get(key).getClass()==JSONObject.class){
         findKeys(obj.get(key),keys);
         }
    }
}

So suppose if your object is {"a":1,"b":{"c":"hello","d":4.0}} the above function should give you ["a","b","c","d"]

But if you want only ["a","c","d"] as your output,you can write-

findKeys(JsonObject obj,List keys){
List<String>keysFromObj=obj.keys();

for(String key:keysFromObj){
    if(obj.get(key).getClass()==JSONObject.class){
         findKeys(obj.get(key),keys);
         }else{
         keys.add(key);
         }
    }
}
like image 77
faizan Avatar answered Feb 23 '23 04:02

faizan


I am late to the party, but I am adding here my solution:

Input:

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}

Retrieve all unique keys:

public static void findAllKeys(Object object, Set<String> finalKeys) {
        if (object instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) object;

            jsonObject.keySet().forEach(childKey -> {
                finalKeys.add(childKey);
                findAllKeys(jsonObject.get(childKey), finalKeys);
            });
        } else if (object instanceof JSONArray) {
            JSONArray jsonArray = (JSONArray) object;

            IntStream.range(0, jsonArray.length())
                    .mapToObj(jsonArray::get)
                    .forEach(o -> findAllKeys(o, finalKeys));
        }
    }

Usage:

Set<String> finalKeys = new HashSet<>();
findAllKeys(new JSONObject(str), finalKeys);
System.out.println(finalKeys);

Output:

[
  GlossEntry,
  GlossSee,
  SortAs,
  GlossList,
  title,
  GlossDiv,
  glossary,
  GlossTerm,
  GlossDef,
  para,
  GlossSeeAlso,
  ID,
  Acronym,
  Abbrev
]

Retrieve all unique "full path" keys:

public void findAllKeys(Object object, String key, Set<String> finalKeys) {
    if (object instanceof JSONObject) {
        JSONObject jsonObject = (JSONObject) object;

        jsonObject.keySet().forEach(childKey -> {
            findAllKeys(jsonObject.get(childKey), key != null ? key + "." + childKey : childKey, finalKeys);
        });
    } else if (object instanceof JSONArray) {
        JSONArray jsonArray = (JSONArray) object;
        finalKeys.add(key);

        IntStream.range(0, jsonArray.length())
                .mapToObj(jsonArray::get)
                .forEach(jsonObject -> findAllKeys(jsonObject, key, finalKeys));
    }
    else{
        finalKeys.add(key);
    }
}

Usage:

Set<String> finalKeys = new HashSet<>();
findAllKeys(new JSONObject(jsonStr), null, finalKeys);
System.out.println(finalKeys);

Output:

[
  glossary.GlossDiv.GlossList.GlossEntry.ID,
  glossary.title,
  glossary.GlossDiv.GlossList.GlossEntry.GlossSee,
  glossary.GlossDiv.GlossList.GlossEntry.GlossTerm, 
  glossary.GlossDiv.GlossList.GlossEntry.Acronym, 
  glossary.GlossDiv.GlossList.GlossEntry.Abbrev, 
  glossary.GlossDiv.GlossList.GlossEntry.GlossDef.para, 
  glossary.GlossDiv.GlossList.GlossEntry.SortAs, 
  glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSeeAlso,
  glossary.GlossDiv.title
]
like image 39
Most Noble Rabbit Avatar answered Feb 23 '23 05:02

Most Noble Rabbit