InfoEngi InfoEngi - 5 months ago 16
Java Question

How do i get, modify and post a json object?

as mentioned in my older topic 3 days ago - Last Topic

i got a json response and changed it to a string. The Json Response represents an User-Object. Within the User-Object i wanted to search for a specific project and delete it. After that, i want to post it again via HttpPost.

private static String getContent(HttpResponse response) {
HttpEntity entity = response.getEntity();
if (entity == null) return null;
BufferedReader reader;
try {
reader = new BufferedReader(new InputStreamReader(entity.getContent()));
String line = reader.readLine();
return line;
} catch (IllegalStateException e) {
} catch (IOException e) {
return null;


String StringResponse = getContent(JsonResponse);
JSONObject jsonObject = new JSONObject(StringResponse);
JSONArray ProjectsArray= jsonObject.getJSONArray("projects");

Searching for a specific project by saving the attributes in a JsonArray.

ArrayList<Integer> indexesToRemove = new ArrayList<Integer>();
for (int i = 0; i < projectsArray.length; i++) {
JSONObject current = projectsArray.get(i);
if (current.get("projectKey") == "**ProjectName**") {

Deleting the project...

for (int i = indexesToRemove.size()-1; i>=0; i--)

That works perfect and my searched project is deleted. But the problem is, that i want to post the modified UserObject/String again via HttpPost. And my deleted project is just in my JsonArray "projectsArray" and not in my string from the beginning. I can't post "projectsArray"....

HttpPost UserChange = new HttpPost (TestUserURL+user); //TODO:
UserChange.setHeader("Accept", "application/json");
UserChange.setHeader("Content-type", "application/json");

params = new StringEntity("ModifiedJsonString", HTTP.UTF_8); // How do i get the complete Json string?

HttpResponse UserChangeResponse = httpclient.execute(UserChange);

HttpEntity entity2 = UserChangeResponse.getEntity();
if (entity2 != null) {

I need the "ModifiedJsonString", which includes the complete json file from the beginning.

params = new StringEntity(ModifiedJsonString, HTTP.UTF_8);

Best Regards


The following code removes one of the selected project.

String jsonString = "{    \"account\": \"Kpatrick\",    \"firstname\": \"Patrick\",    \"instances\": [        {            \"id\": \"packerer-pool\",            \"key\": \"packerer-pool123\",            \"userAccount\": \"kpatrick\",            \"firstname\": \"Patrick\",            \"lastname\": \"Schmidt\"        }    ],    \"projects\": [        {            \"id\": \"packerer-projectPool\",            \"projectKey\": \"projectPool-Pool\",            \"cqprojectName\": \"xxxxx\"        },        {            \"id\": \"packerer-secondproject\",            \"projectKey\": \"projectPool-Pool2\",            \"cqprojectName\": \"xxxx\"        },        {            \"id\": \"packerer-thirdproject\",            \"projectKey\": \"projectPool-Pool3\",            \"cqprojectName\": \"xxxx\"        }    ],    \"clients\": [],    \"dbid\": 76864576,    \"version\": 1,    \"id\": \"dbpack21\"}";
JSONParser parser = new JSONParser();
JSONObject jsonObject = (JSONObject) parser.parse(jsonString);

ArrayList<String> listOfNodes = new ArrayList<String>();
JSONArray projectArray = (JSONArray) jsonObject.get("projects");
int len = projectArray.size();
if (projectArray != null) {
    for (int i = 0; i < len; i++) {
        String projectId = ((JSONObject) projectArray.get(i)).get("projectKey").toString();
        if (!projectId.equals("projectPool-Pool2")) {


// Remove the element from arraylist
// Recreate JSON Array
JSONArray jsArray = new JSONArray();
jsonObject.put("projects", listOfNodes);


This for example , prints the following JSON string removing one of the projects.Linted JSON Once you have this , you can then use this to create a StringEntity and then use it in HTTPPost calls. Hope it helps