2

I need to get the keys of a JSON. How can I achieve that?

For example,

{
"empId":"123",
"emp_name":"asda",
"emp_address":"skdjahskdga"
}

In the above code, I need to get empId,emp_name and emp_address.

Suppose it is an array like this,

{"products":[{
"empId":"123",
"emp_name":"asda",
"emp_address":"skdjahskdga"
},
{
"empId":"123",
"emp_name":"asda",
"emp_address":"skdjahskdga"
}]}

I need to get products.empId, products.emp_name, products.emp_address.

If there is one more level, say employees array contains products array, and in turn products contains empId,emp_name and emp_address, I need to get employees.product1.empId, employees.product1.emp_name and employees.product1.emp_address, and so on.. Please let me know how to achieve this.

NaveenBharadwaj
  • 1,212
  • 5
  • 19
  • 42
  • share your code of how you have tried it and what error did you get? – Dude May 13 '15 at 08:05
  • Not sure what you really trying to achieve, but maybe this could help you (http://stackoverflow.com/questions/19195492/extracting-keys-from-a-jsonobject-using-keyset) – ceekay May 13 '15 at 08:07

2 Answers2

1

You could use a library like GSon that does this for you. If you create a serializable Product class with the fields you want then all you have to do is

json = {"products":[{
             "empId":"123",...

Product[] videoArray = gson.fromJson(json, Product[].class); 
idipous
  • 2,868
  • 3
  • 30
  • 45
0
static Set<String> finalListOfKeys = new LinkedHashSet<String>();

public static void main(String[] args) {
    JSONParser parser = new JSONParser();

    try {

        Object obj = parser.parse(new FileReader("Absolute Path of json file"));
        JSONObject jsonObject = (JSONObject) obj;
        Set<String> jsonKeys = jsonObject.keySet();
        for (String key : jsonKeys) {
            Object val = jsonObject.get(key);
            if (val instanceof JSONArray) {
                JSONArray array = (JSONArray) val;
                jsonArray(array, key);

            } else if (val instanceof JSONObject) {
                JSONObject jsonOb = (JSONObject) val;
                jsonObj(jsonOb, key);
            } else {
                finalListOfKeys.add(key);
                System.out.println("Key is : " + key);
            }
        }

        System.out.println("Final List : " + finalListOfKeys);

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

public static void jsonObj(JSONObject object, String key2) {
    Set<String> innerKeys = object.keySet();
    System.out.println("Inner Keys : " + innerKeys);
    for (String key : innerKeys) {
        System.out.println("Key : " + key);
        Object val = object.get(key);
        if (val instanceof JSONArray) {
            JSONArray array = (JSONArray) val;
            jsonArray(array, key2 + "->" + key);

        } else if (val instanceof JSONObject) {
            JSONObject jsonOb = (JSONObject) val;
            jsonObj(jsonOb, key2 + "->" + key);
        } else {
            finalListOfKeys.add(key2 + "->" + key);
            System.out.println("Key is : " + key2 + "->" + key);
        }
    }
}

public static void jsonArray(JSONArray array, String key) {
    if (array.size() == 0) {
        finalListOfKeys.add(key);
    } else {

        for (int i = 0; i < array.size(); i++) {
            Object jObject = array.get(i);
            if (jObject instanceof JSONObject) {
                JSONObject job = (JSONObject) jObject;
                System.out.println(job);
                jsonObj(job, key);

            }
        }
    }
}
  • 1
    You should really add some explanation as to why this should work - you can also add code as well as the comments in the code itself - in its current form, it does not provide any explanation which can help the rest of the community to understand what you did to solve/answer the question. This is especially important for an older question and the questions that already have answers. – ishmaelMakitla Feb 03 '17 at 11:01
  • Thanks for the feedback.The code is pretty self-explanatory,hence I refrained from any comments. – Saptarshi Mitra Feb 03 '17 at 11:43