javajsonqjsonobject

Delete a child attribute from json file


I have the following HTTP JSON-response in Java, which represents a user object.

{
    "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"
}

Now, I want to search a specific project with the help of the projectkey (for example "projectPool-Pool2"). After that, I want to delete the element completely. Because my target is to send a HTTP post-call without this project.

The result should be similar to below for my HTTP post-call:

    {
    "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-thirdproject",
            "projectKey": "projectPool-Pool3",
            "cqprojectName": "xxxx",
        }
    ],

    "clients":
    [
    ],
    "dbid": 76864576,
    "version": 1,
    "id": "dbpack21"
}

First i have parsed the response to a string.

    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();
        reader.close();
        return line;
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

And now i am trying to search the specific project, but i don't know how to continue.

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

Is that approach correct?

Best Regards!


Solution

  • Once you have your array, try something like...

    // Array to store the indexes of the JSONArray to remove
    ArrayList<Integer> indexesToRemove = new ArrayList<Integer>();
    // Iterate through projects array, check the object at each position
    // if it contains the string you want, add its index to the removal list
    for (int i = 0; i < projectsArray.length; i++) {
        JSONObject current = projectsArray.get(i);
        if (current.get("projectKey") == "**DESIRED PROJECT KEY**") {
            indexesToRemove.add(i);
        } 
    }
    

    Now you can iterate through your indexes to remove, and remove the corresponding object from the array with the JSONArrays remove method (not sure what it is called, the code above is from memory). Make sure to remove your items BACKWARDS, otherwise you will delete earlier items, which will change the indexes, resulting in your removing an incorrect item if you then remove another index.

    // Going through the list backwards so we can remove the highest item each 
    //time without affecting the lower items
    for (int i = indexesToRemove.size()-1; i>=0; i--) {
        projectsArray.remove(indexesToRemove.get(i));
    }