0

how can I create a JSON Object like the following, in Java using JSONObject ?

{
    "fields": {
     "issuetype":{"id": "10004"},
     "project":{"key": "TES"},
     "reporter":{"name":"TestUser"},
     "summary":"Screen not responding",
     "description":"New Bug in UI. Screen not responding",
     "assignee":{"name":"Test"}

     }
}

What I have tried so far

JsonObject issuetype = new JsonObject();
        issuetype.addProperty("id", "10004");
        JsonObject project = new JsonObject();
        project.addProperty("key", "TES");
        JsonObject reporter = new JsonObject();
        reporter.addProperty("name", "TestUser");
        JsonObject summary = new JsonObject();
        summary.addProperty("summary", "Screen not responding");
        JsonObject description = new JsonObject();
        description.addProperty("description", "New Bug in UI. Screen not responding");
        JsonObject assignee = new JsonObject();
        assignee.add("name", "Test");

can anybody help me to solve this.?

Thanks

2 Answers 2

1

You should use Json factory class to create object builders:

JsonObject issuetype = Json.createObjectBuilder()
    .add("fields", Json.createObjectBuilder()
        .add("issuetype", Json.createObjectBuilder().add("id", "10004"))
        .add("project", Json.createObjectBuilder().add("key", "TES"))
        .add("reporter", Json.createObjectBuilder().add("name", "TestUser"))
        .add("summary", "Screen not responding")
        .add("description", "New Bug in UI. Screen not responding")
        .add("assignee", Json.createObjectBuilder().add("name", "Test"))
    )
    .build();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @barsik let me try this.
0

Try following,

        JsonObject issuetype = new JsonObject();
        issuetype.addProperty("id", "10004");
        JsonObject project = new JsonObject();
        project.addProperty("key", "TES");
        JsonObject reporter = new JsonObject();
        reporter.addProperty("name", "TestUser");
        JsonObject summary = new JsonObject();
        summary.addProperty("summary", "Screen not responding");
        JsonObject description = new JsonObject();
        description.addProperty("description", "New Bug in UI. Screen not responding");
        JsonObject assignee = new JsonObject();
        assignee.addProperty("name", "Test");

        JsonObject field = new JsonObject();
        field.add("issuetype", issuetype);
        field.add("project", project);
        field.add("reporter", reporter);
     // field.add("summary", summary);
        field.addProperty("summary", "Screen not responding");
        field.add("description", description);
        field.add("assignee", assignee);

        JsonObject fields = new JsonObject();
        fields.add("fields", field);

        System.out.println(fields.toString());

OutPut :

{
    "fields": {
        "issuetype": {
            "id": "10004"
        },
        "project": {
            "key": "TES"
        },
        "reporter": {
            "name": "TestUser"
        },
        "summary": {
            "summary": "Screen not responding"
        },
        "description": {
            "description": "New Bug in UI. Screen not responding"
        },
        "assignee": {
            "name": "Test"
        }
    }
}

3 Comments

Thanks @RakeshKR that helped.
I think "summary":"Screen not responding", is not same as "summary": { "summary": "Screen not responding" }, Please check
@Raghavendra change field.add("summary", summary); as field.addProperty("summary", "Screen not responding"); for supporting that.

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.