2

Hi I have a Json like follows:

A {
    B {
        C [{
            x1:x2,
            y1:y2
          }]
       }
  }   

Now my requirement is to convert the A, B, C to camelCase notation(as required) which I did by using @JsonProperty and getter and setter methods for the respective POJO class.

Finally, my output is as follows:

a {
    b {
        c [{
            x1:x2,
            y1:y2
          }]
       }
  }   

Now I want to add a new empty object like d between b and c.

Can you please help me in appending a new empty JSON object.

The required output should be something like this:

a {
    b {
       d {
          c [{
            x1:x2,
            y1:y2
          }]
       }
     }
  }

Json :

"Skills" : {
  "SkillSet" : [ 
    {
        "Skill" : "Management"
    },
    {
        "Skill" : "IT"
    }, 
  ]
}

I need something like this:

"skills" : {
  "mainSkills" {
     "skillSet" : [ 
       {
        "Skill" : "Management"
       },
       {
        "Skill" : "IT"
       }, 
     ]
   }
}

I'm done with converting the camelCase but I need to insert the new object "mainSkills".

4
  • 1
    Can you please post a valid JSON? Aug 16, 2017 at 12:07
  • @CássioMazzochiMolin Updated the JSON.
    – Pavan
    Aug 16, 2017 at 12:26
  • Still not a valid JSON though. Aug 16, 2017 at 12:27
  • Map your source JSON to Java classes (let's call them A). Map your target JSON to Java classes (let's call them B). Parse your source JSON to A, transform A to B, and write B as JSON.
    – JB Nizet
    Aug 16, 2017 at 12:36

1 Answer 1

1

The following will give you the expected output:

String json = "{\n" +
              "  \"skills\": {\n" +
              "    \"skillSet\": [\n" +
              "      {\n" +
              "        \"skill\": \"Management\"\n" +
              "      },\n" +
              "      {\n" +
              "        \"skill\": \"IT\"\n" +
              "      }\n" +
              "    ]\n" +
              "  }\n" +
              "}";

// Create ObjectMapper instance to parse the JSON
ObjectMapper mapper = new ObjectMapper();

// Parse the JSON into the Jackson tree model
JsonNode tree = mapper.readTree(json);

// Store a reference to the "skills" node
JsonNode skills = tree.get("skills");

// Store a reference to the "skillsSet" node
JsonNode skillSet = skills.get("skillSet");

// Remove the "skillsSet" node from the tree
((ObjectNode) skills).remove("skillSet");

// Create the "skillSet" node under "mainSkills" 
// and sets it under the "skills" node
((ObjectNode) skills).set("mainSkills", 
        mapper.createObjectNode().set("skillSet", skillSet));

// Write the tree as a JSON string using the default pretty printer
String newJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(tree);

If you don't want to manipulate the JSON as demonstrated above, follow the JB Nizet approach.

2
  • Thank you so much. It is working as expected...All I need to learn about the code which you did step by step.
    – Pavan
    Aug 17, 2017 at 6:03
  • @Pavan I added comments to my code to make it clear. Aug 17, 2017 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.