Reputation: 103
I want to merge JSON Object to master JSON Object at required depth
Master JSON
{
"data":{
"barrel":{
"length":100,
"stage":[0,1,2]
},
"a":100,
"b":200
}
}
New JSON Object to be added at path data.barrel
{
"width":100
}
Required Output
{
"data":{
"barrel":{
"length":100,
"stage":[0,1,2],
"width":100
},
"a":100,
"b":200
}
}
Tried following code
String str = "{\"data\":{\"barrel\":{\"length\":100,\"stage\":[0,1,2]},\"a\":100,\"b\":200}}";
String newstr = "{\"width\":100}";
String path = "data.barrel";
JSONObject jonew = new JSONObject(newstr);
Josson josson = Josson.fromJsonString(str);
Josson jossonNew = Josson.fromJsonString(jonew.toString());
System.out.println(josson.getNode(path));
JsonNode jn = josson.getNode(path);
JsonNode jnNew = jossonNew.getNode();
Iterator<String> keys = jonew.keys();
while (keys.hasNext()) {
String key = keys.next();
if (!jn.has(key))
((ObjectNode) jn).set(key, jnNew.get(key));
}
Output
{"length":100,"stage":[0,1,2],"width":100}
I am able to add child as a separate object, but not to the parent JSON Object I need a generic way as the path and depth can be varying
Regards, Pranav
Upvotes: 0
Views: 210
Reputation: 1271
Deserialization
Josson master = Josson.fromJsonString(
"{" +
" \"data\":{" +
" \"barrel\":{" +
" \"length\":100," +
" \"stage\":[0,1,2]" +
" }," +
" \"a\":100," +
" \"b\":200" +
" }" +
"}");
By Parameters
Map<String, JsonNode> params = new HashMap<>();
params.put("$new", Josson.readJsonNode("{\"width\":100}"));
JsonNode node = master.getNode(
"field(data.field(barrel.mergeObjects(?, $new)))", params);
System.out.println(node.toPrettyString());
By Inline
String addNew = "{\"width\":100}"; // any ' needs to double it ''
JsonNode node = master.getNode(
"field(data.field(barrel.mergeObjects(?, json('" + addNew + "'))))");
System.out.println(node.toPrettyString());
Output
{
"data" : {
"barrel" : {
"length" : 100,
"stage" : [ 0, 1, 2 ],
"width" : 100
},
"a" : 100,
"b" : 200
}
}
Upvotes: 0
Reputation: 31
public void mergeJSON(JsonObject masterJson, String path, JsonObject newJson)
{
String[] keys = path.split("\\.");
JsonObject currentJson = masterJson;
for (int i = 0; i < keys.length - 1; i++) {
String key = keys[i];
if (currentJson.has(key) && currentJson.get(key).isJsonObject()) {
currentJson = currentJson.getAsJsonObject(key);
} else {
JsonObject nestedJson = new JsonObject();
currentJson.add(key, nestedJson);
currentJson = nestedJson;
}
}
String lastKey = keys[keys.length - 1];
currentJson.add(lastKey, newJson);
}
and then you can invoke this method
mergeJSON(masterJson, "data.barrel", newJson);
String updatedJsonString = gson.toJson(masterJson);
System.out.println(updatedJsonString);
Upvotes: 0