Mistu4u
Mistu4u

Reputation: 5416

How to create JSON for multiple nodes for a single key with jackson

I want to create a JSON like the following using Jackson in Java. I am able to do it but for a single key. In this case "TestProject1-staging" it is not able to save "children" and "vars" simultaneously.

 "TestProject1-staging": {
    "children" : [ "TestProject1-staginga", "TestProject1-stagingb", "TestProject1-stagingc" ],
    "vars": {
        "projects": {
            "TestProject1": {
                "app_tier": "apptier",
                "remote_dir": "/release/Test1"
            }
        }
    }
}

This is the code that I wrote:

ObjectMapper mapper = new ObjectMapper();
ObjectNode projects = mapper.createObjectNode();
ObjectNode finalObj = mapper.createObjectNode();
ObjectNode proRootNode = mapper.createObjectNode();
ObjectNode children = mapper.createObjectNode();
ObjectNode proRoot = mapper.createObjectNode();
proRoot.put("app_tier", inventoryContainer.appTier);
proRoot.put("remote_dir", inventoryContainer.remoteDirectory);
proRootNode.set(projectRoot, proRoot);
projects.set("projects", proRootNode);
children.put("children", 
mapper.writerWithDefaultPrettyPrinter().writeValueAsString(inventoryContainer.groups));
stagingVarNotSet = false;
finalObj.set(inventoryContainer.projectName, children);
varNode.set("vars", projects);
//finalObj.set(inventoryContainer.projectName, varNode);
System.out.println(StringEscapeUtils.unescapeJava(
     mapper.writerWithDefaultPrettyPrinter().writeValueAsString(finalObj)));

As you can see, the commented line tries to set the vars variable. If I uncomment it, vars will be printed but children will be lost. In the current format, it prints the children but not the vars.

So how can I print both of them together?

Upvotes: 1

Views: 2698

Answers (1)

Kousik Mandal
Kousik Mandal

Reputation: 720

Check if this works for you.

JSonCreator .java

package json;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class JSonCreator {
    public static void main(String[] args) {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode rootNode = mapper.createObjectNode();

        // Create TestProject1 JSON Object
        ObjectNode testProject1Node = mapper.createObjectNode();
        testProject1Node.put("app_tier", "apptier");
        testProject1Node.put("remote_dir", "/release/Test1");

        // Create projects JSON Object
        ObjectNode projectsNode = mapper.createObjectNode();
        projectsNode.set("TestProject1", testProject1Node);

        // Create vars JSON Object
        ObjectNode varsNode = mapper.createObjectNode();
        varsNode.set("projects", projectsNode);

        // Create children JSON Array
        ArrayNode childrenArrayNode = mapper.createArrayNode();
        childrenArrayNode.add("TestProject1-staginga");
        childrenArrayNode.add("TestProject1-stagingb");
        childrenArrayNode.add("TestProject1-stagingc");

        // Create children JSON object
        ObjectNode childrenNode = mapper.createObjectNode();
        childrenNode.set("children", childrenArrayNode);

        // Create TestProject1-staging" JSON object
        ObjectNode testProject1stagingNode = mapper.createObjectNode();
        testProject1stagingNode.set("children", childrenArrayNode);
        testProject1stagingNode.set("vars", varsNode);

        // append into root node
        ((ObjectNode) rootNode).set("TestProject1-staging",
                testProject1stagingNode);

        // convert ObjectNode to pretty-print JSON
        String json = null;
        try {
            json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(
                    rootNode);
            // print json
            System.out.println(json);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
}

Output:

{
  "TestProject1-staging" : {
    "children" : [ "TestProject1-staginga", "TestProject1-stagingb", "TestProject1-stagingc" ],
    "vars" : {
      "projects" : {
        "TestProject1" : {
          "app_tier" : "apptier",
          "remote_dir" : "/release/Test1"
        }
      }
    }
  }
}

Upvotes: 2

Related Questions