简体   繁体   中英

How to add a child object in a json array using java

I have a json array like this:

data:

     [
       {
         image_url:"www",
         testimonial_text: "The standard chunk of Lorem Ipsum used since the,
         name: "Rohith",
         designation: "Architect"
       }
      ]

But I need to convert this using java like this:

data:

    [
     {
         image_url:"www",
         testimonial_text: "The standard chunk of Lorem Ipsum used since the,
         name: "Rohith",
         data2:[{ designation: "Architect"}]
     }
      ]

Using Jackson JSON API parser, https://github.com/FasterXML/jackson

    String json = " [{ image_url:\"www\", testimonial_text: \"The standard chunk of Lorem Ipsum used since the\", name: \"Rohith\", designation: \"Architect\" }, "
            + "{ image_url:\"www\", testimonial_text: \"The standard chunk of Lorem Ipsum used since\", name: \"Rohith\", designation: \"Architect\" }]";

    byte[] jsonData = json.getBytes();

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);

    // create JsonNode
    JsonNode rootNode = objectMapper.readTree(jsonData);

    ObjectNode createDesignationNode = objectMapper.createObjectNode();
    Iterator<JsonNode> iterator = rootNode.iterator();

    while (iterator.hasNext()) {
        JsonNode jsonNode = (JsonNode) iterator.next();
        createDesignationNode.put("designation", jsonNode.get("designation"));

        ArrayNode createData2Array = objectMapper.createArrayNode();
        createData2Array.add(createDesignationNode);

        ((ObjectNode) jsonNode).remove("designation");
        ((ObjectNode) jsonNode).put("data2", createData2Array);

    }

    PrintStream out = System.out;
    objectMapper.writeValue(out, rootNode);

RESULT [{"image_url":"www","testimonial_text":"The standard chunk of Lorem Ipsum used since the","name":"Rohith","data2":[{"designation":"Architect"}]},{"image_url":"www","testimonial_text":"The standard chunk of Lorem Ipsum used since","name":"Rohith","data2":[{"designation":"Architect"}]}]

    JsonArray data = new JsonArray("<Data String>");

    JsonObject data2 = new JsonObject ();
    data2 .put("designation" , "Architect");

    JSONArray arr = new JSONArray();
    arr .put(data2 );

    data.getJSONObject(0).put("data2" , arr );

I strongly suggest you using the latest standard JavaEE8 API JSON-P 1.1 ( JSR374 )

Glassfish already has an implementation.

Maven dependency :

<dependency>
    <groupId>javax.json</groupId>
    <artifactId>javax.json-api</artifactId>
    <version>1.1</version>
</dependency>

<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.json</artifactId>
    <version>1.1</version>
</dependency>

Or you can download the following two jars directly and import into your project :

javax.json-api-1.1.2.jar

javax.json-1.1.2.jar

Here is the runnable code snapshot :

String data = "[{\"image_url\":\"www\","
                + "\"testimonial_text\": \"The standard chunk of Lorem Ipsum used since the\","
                + " \"name\": \"Rohith\","
                + "\"designation\": \"Architect\"}]";
        JsonArray original = Json.createReader(new StringReader(data)).readArray();
        JsonArray changes = Json.createArrayBuilder().add(Json.createObjectBuilder().add("designation", "Architect"))
                .build();
        JsonPatchBuilder builder = Json.createPatchBuilder();
        JsonArray target = builder.remove("/0/designation").add("/0/data2", changes).build().apply(original);
        System.out.println(target.toString());

Note : You need add double quotation marks at the key field

Here is the output :

[{"image_url":"www","testimonial_text":"The standard chunk of Lorem Ipsum used since the","name":"Rohith","data2":[{"designation":"Architect"}]}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM