Home > database >  How to get all key and values from nested JSON in java
How to get all key and values from nested JSON in java

Time:08-28

Hi I need to read all key, values from nested JSON, where ever there is inner JSON. I need that values ignoring the key.From below JSON i need Key values for nested JSON, like: responseStatus-passed, "statusCode":"200", "retrieveQuoteResponse":null,"quoteGuid":null, etc.ignoring the start key value like: responsePreamble, quoteProductList which has a nested json inside it.

{
    "responsePreamble": {
        "responseStatus": "Passed",
        "statusCode": "200",
        "responseMessage": "Records Found"
    },
    "retrieveQuoteResponse": null,
    "totalQuoteProductCount": 2,
    "quoteProductList": {
        "quoteGuid": null,
        "quantity": 180
}

Code:

ObjectReader reader = new ObjectMapper().readerFor(Map.class); 
Map<String, Map<String, String>> employeeMap = reader.readValue(jsonObject); 
for (Entry<String, Map<String, String>> empMap : employeeMap.entrySet()) { 
    Map<String, String> addMap = empMap.getValue(); 
    if(addMap!=null) { 
        for (Entry<String, String> addressSet : addMap.entrySet()) {
            System.out.println(addressSet.getKey()   " :: "   addressSet.getValue()); 
        } 
    } 
}

OutPut:

responseStatus :: Passed
statusCode :: 200
responseMessage :: Records Found
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map
    at com.im.api.tests.CompareTwoJsons.main(CompareTwoJsons.java:78)

CodePudding user response:

For your specific case, the following code will do the job:

 String json = "{\n"  
            "   \"responsePreamble\":{\n"  
            "      \"responseStatus\":\"Passed\",\n"  
            "      \"statusCode\":\"200\",\n"  
            "      \"responseMessage\":\"Records Found\",\n"  
            "      \"accountInfo\":{\n"  
            "         \"id\":\"631b3d5b-62c8-e711-80f3-3863bb343ba0\"\n"  
            "      },\n"  
            "      \"account\":\"40114570\",\n"  
            "      \"contactInfo\":{\n"  
            "         \"id\":\"1af63ebb-2680-eb11-a812-000d3a4e381d\"\n"  
            "      },\n"  
            "      \"contact\":\"Kenny Tokuda\",\n"  
            "      \"currencyInfo\":{\n"  
            "         \"id\":\"8c2ef-\",\n"  
            "         \"symbol\":\"$\"\n"  
            "      },\n"  
            "      \"vendorCurrencyInfo\":{\n"  
            "         \"id\":null\n"  
            "      }\n"  
            "   },\n"  
            "   \"retrieveQuoteResponse\":null,\n"  
            "   \"totalQuoteProductCount\":2,\n"  
            "   \"quoteProductList\":{\n"  
            "      \"quoteGuid\":null,\n"  
            "      \"quantity\":180\n"  
            "   }\n"  
            "}";
        ObjectMapper mapper = new ObjectMapper();
    JsonNode jsonNode = mapper.readTree(json);
    Iterator<String> iterator = jsonNode.fieldNames();
    while (iterator.hasNext()) {
        String key = iterator.next();
        printRec(jsonNode, key);
    }

Here is how function printRec looks like:

public static void printRec(JsonNode jsonNode, String key) {
        JsonNode node = jsonNode.get(key);
        if (node.isObject()) {
            Iterator<Map.Entry<String, JsonNode>> fields = node.fields();
            fields.forEachRemaining(field -> {
                printRec(node, field.getKey());
                if (!field.getValue().isObject()) {
                    System.out.println(field.getKey()   " : "   field.getValue());
                }
            });
        }
    }

When you run this code you should see the following output:

responseStatus : "Passed"
statusCode : "200"
responseMessage : "Records Found"
id : "631b3d5b-62c8-e711-80f3-3863bb343ba0"
account : "40114570"
id : "1af63ebb-2680-eb11-a812-000d3a4e381d"
contact : "Kenny Tokuda"
id : "8c2ef-"
symbol : "$"
id : null
quoteGuid : null
quantity : 180
  • Related