Gikar
Gikar

Reputation: 219

Create Json String Dynamically using Java API's?

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();                      

Upvotes: 0

Views: 674

Answers (2)

Ejaz Ahmed
Ejaz Ahmed

Reputation: 339

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);
    }
}

Upvotes: 1

Syam S
Syam S

Reputation: 8499

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);
    }
}

Upvotes: 0

Related Questions