0

I want to create dynamic string like this using java api's, I tried using javax.json.Json. Facing issues with the creation

{
    "name": "Heirachy",
    "parents": [
        {
            "name": "Parent1",
            "isparent": true
        },
        {
            "name": "Parent2",
            "isparent": true
        },
        {
            "name": "Parent3",
            "isparent": true,
            "parents": [
                { 
                    "name" : "inner-parent3",
                    "isparent" : true,
                    "parents":[
                        {
                            "name" : "inner-most-parent3",
                            "isparent" : true
                        }
                    ]
                },
                { 
                    "name" : "outer-parent3",
                    "isparent" : true
                }
            ]
        }
    ],
    "children": [
        {
            "name": "Childern1",
            "isparent": false
        },
        {
            "name": "Childern2",
            "isparent": false
        },
        {
            "name": "Childern3",
            "isparent": false,
            "children": [
                { 
                    "name" : "inner-1-children3",
                    "isparent" : false
                },
                { 
                    "name" : "inner-2-children3",
                    "isparent" : false
                }
            ]
        },
        {
            "name": "Childern4",
            "isparent": false
        }
    ]
}
JsonArray parent = Json.createArrayBuilder().add(Json.createObjectBuilder()
.add("name", "Parent-1").add("isparent", "true"))
.add(Json.createObjectBuilder().add("name", "Parent-2").add("isparent", "true")).build();

JsonArray children = Json
                .createArrayBuilder()
                .add(Json.createObjectBuilder().add("name", "Children-1")
                        .add("isparent", "true"))
                .add(Json.createObjectBuilder().add("name", "Children-2")
                        .add("isparent", "true")).build();  </pre>

JsonObject obj = Json.createObjectBuilder().add("name", "Heirachy")
                .add("parents", parent).add("children", children).build();                      
4
  • 1
    Can you post the code you tried? Commented Aug 26, 2015 at 11:18
  • 1
    And more information regarding the issues you encountered? Commented Aug 26, 2015 at 11:19
  • Please find the code snippet inline Commented Aug 26, 2015 at 11:29
  • 1
    "Facing issues with the creation " Is that all? No info about the issues? Commented Aug 26, 2015 at 11:57

2 Answers 2

1

I hope this one helps

import com.fasterxml.jackson.databind.ObjectMapper;

class A {

    public static String jsonify(Object message) {
        return objectMapper.writeValueAsString(message);
    }

    public void someMethod() {
        //request can be any DTO
        String s = jsonify(request);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Using Jackson you could do like

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class JsonSample {

    public static void main(String[] args) {
        JsonNodeFactory nodeFactory = JsonNodeFactory.instance;

        ObjectNode heirachy = nodeFactory.objectNode();
        ArrayNode parents = nodeFactory.arrayNode();
        //Parents
        ObjectNode parent1 = nodeFactory.objectNode();
        parent1.put("name", "Parent1");
        parent1.put("isParent", "true");
        ObjectNode parent2 = nodeFactory.objectNode();
        parent2.put("name", "Parent2");
        parent2.put("isParent", "true");
        ObjectNode parent3 = nodeFactory.objectNode();
        parent3.put("name", "Parent3");
        parent3.put("isParent", "true");
        ArrayNode parent3Arr = nodeFactory.arrayNode();
        ObjectNode innerParent3 = nodeFactory.objectNode();
        innerParent3.put("name", "Inner-Parent3");
        innerParent3.put("isParent", "true");
        ArrayNode innerParent3Arr = nodeFactory.arrayNode();
        ObjectNode innermostParent3 = nodeFactory.objectNode();
        innermostParent3.put("name", "Inner-most-Parent3");
        innermostParent3.put("isParent", "true");
        innerParent3Arr.add(innermostParent3);
        innerParent3.put("parents", innerParent3Arr);
        parent3Arr.add(innerParent3);
        parent3.put("parents", parent3Arr);
        parents.add(parent1);
        parents.add(parent2);
        parents.add(parent3);
        //Children
        ArrayNode children = nodeFactory.arrayNode();
        ObjectNode child1 = nodeFactory.objectNode();
        child1.put("name", "Child1");
        child1.put("isParent", "false");
        ObjectNode child2 = nodeFactory.objectNode();
        child2.put("name", "Child2");
        child2.put("isParent", "false");
        ObjectNode child3 = nodeFactory.objectNode();
        child3.put("name", "Child3");
        child3.put("isParent", "false");
        ArrayNode child3Arr = nodeFactory.arrayNode();
        ObjectNode innerChild3 = nodeFactory.objectNode();
        innerChild3.put("name", "Inner-Child3");
        innerChild3.put("isParent", "false");
        ArrayNode innerChild3Arr = nodeFactory.arrayNode();
        ObjectNode innermostChild3 = nodeFactory.objectNode();
        innermostChild3.put("name", "Inner-most-Child3");
        innermostChild3.put("isParent", "false");
        innerChild3Arr.add(innermostChild3);
        innerChild3.put("children", innerChild3Arr);
        child3Arr.add(innerChild3);
        child3.put("children", child3Arr);
        children.add(child1);
        children.add(child2);
        children.add(child3);
        heirachy.put("name", "Hierarchy");
        heirachy.put("parents", parents);
        heirachy.put("children", children);
        System.out.println(heirachy);
    }
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.