javajsonjsonnode

Treves the JSON object and manipulate the value in java


What im trying to do is

JSON:

{
aKey:{
        aChildKey:""
     },
bKey:""
}

expected:

aKey:{
        aChildKey:"aKey.aChildKey"
     },
bKey:"bKey"
}

Please can some one help me in getting the expected the value


Solution

  • You need to deserialize the JSON into an object, set the values, then serialize it back into JSON. There are a number of libraries you can use for this, like org.json, gson, or Jackson. Those libraries also allow you to modify the value directly. For example, using org.json, you can do something like this:

    JSONObject jsonObject = new JSONObject(myJsonString);
    jsonObject.getJSONObject("akey").put("aChildKey","aKey.aChildKey");
    

    See How to parse JSON in Java