-1

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"}]
     }
      ]
Thientvse
  • 1,753
  • 1
  • 14
  • 23
Manju
  • 100
  • 3
  • 15

3 Answers3

1

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"}]}]

Rajesh
  • 400
  • 3
  • 7
  • If I have a single data array it's coming fine.But when I try to load multiple data arrays it looks like [ { image_url:"www", testimonial_text: "The standard chunk of Lorem Ipsum used since, name: "Rohith", data2:[{ designation: "Architect"}] } { image_url:"www", testimonial_text: "The standard chunk of Lorem Ipsum used since, name: "Rohith", designation: "Architect" } ] – Manju Nov 09 '17 at 04:14
  • can you paste the results how its look – Rajesh Nov 09 '17 at 04:16
  • [ { image_url:"www", testimonial_text: "The standard chunk of Lorem Ipsum used since, name: "Rohith", data2:[{ designation: "Architect"}] } { image_url:"www", testimonial_text: "The standard chunk of Lorem Ipsum used since, name: "Rohith", designation: "Architect" } ] – Manju Nov 09 '17 at 04:18
  • check edited answer. I've updated in original answer. – Rajesh Nov 09 '17 at 04:55
0
    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 );
J.R
  • 2,113
  • 19
  • 21
  • JsonArray data = new JsonArray(""); what do I need to pass in data String can you please elaborate. I am very basic to this? – Manju Nov 08 '17 at 07:26
  • if you getting the data as string pass to this constructor. In your case the Data String is "[ { image_url:"www", testimonial_text: "The standard chunk of Lorem Ipsum used since the, name: "Rohith", designation: "Architect" } ]" – J.R Nov 08 '17 at 07:30
  • data.recs.getJSONObject(0).put("data2" , arr ); I am getting error at "recs" what is it exactly? – Manju Nov 08 '17 at 07:44
0

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"}]}]

wodong
  • 297
  • 2
  • 10