Removing fields from json string

Viewed 987

I am writing api tests. I am using rest assured to make the requests the following way:

    public void POSTNewRequest(String endpoint, String requestBody){
        response = given().auth().none()
                .header("Content-Type", "application/json")
                .contentType(ContentType.JSON)
                .when()
                .body(requestBody).log().all()
                .post(endpoint);
    }

The requestBody I'm passing in the request is constructed by converting custom java objects to a string using ObjectMapper.writeValueAsString(requestBody).

This method has been working great for me, but now I need to make a bunch of requests where there is a certain field missing. eg:

{
    "foo": [
        {
            "description": "dflt desc",
            "ref": "abcd",            
            "FIELDTOREMOVE": 0,
            "customArray": {
                "number": 22,
                "letter": "B"
            }
        }
    ],
    "moreInfo": {
        "email": "test@test.be",
        "name": "Jhon Doe"
    }
}

Now I would like to remove the field "FIELDTOREMOVE" inside this request just before the post method. I tried to convert the requestBody string to a JsonNode and then removing the field but it doesn't remove the field.

    private void removeNullFields(String requestBody) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readTree(requestBody);
        System.out.println(jsonNode.get("FIELDTOREMOVE"));
        ((ObjectNode)jsonNode).remove("FIELDTOREMOVE");
    }

And when I try to print the value the field its returning "null" so I'm obviously doing something wrong... I also tried achieving the same by using the gson library with similar results so I guess there is a misunderstanding on my end but can't figure out where to look to fix my problem.

In short: I'm making api requests using the rest assured library by passing a string as the body but in this string I sometimes have to remove certain fields the check what response I'm getting.

2 Answers

The "foo" in your JSON is an array. You should do:

ObjectMapper mapper = new ObjectMapper();

JsonNode jsonNode = mapper.readTree(requestBody);

jsonNode.get("foo").forEach(e -> ((ObjectNode) e).remove("FIELDTOREMOVE"));

System.out.println(jsonNode.toPrettyString());

Output:

{
    "foo" : [ {
        "description" : "dflt desc",
        "ref" : "abcd",
        "customArray" : {
            "number" : 22,
            "letter" : "B"
        }
    } ],
    "moreInfo" : {
        "email" : "test@test.be",
        "name" : "Jhon Doe"
    }
}

You can use this library:

<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20210307</version>
</dependency>

So that the code would look like:

import org.json.JSONObject;

public class JSONManipulation {

    static final String JSON = "{\n" +
            "    \"foo\": [\n" +
            "        {\n" +
            "            \"description\": \"dflt desc\",\n" +
            "            \"ref\": \"abcd\",            \n" +
            "            \"FIELDTOREMOVE\": 0,\n" +
            "            \"customArray\": {\n" +
            "                \"number\": 22,\n" +
            "                \"letter\": \"B\"\n" +
            "            }\n" +
            "        }\n" +
            "    ],\n" +
            "    \"moreInfo\": {\n" +
            "        \"email\": \"test@test.be\",\n" +
            "        \"name\": \"Jhon Doe\"\n" +
            "    }\n" +
            "}";

    public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject(JSON);
        System.out.println("Value before edit: " + jsonObject.toString());
        jsonObject.getJSONArray("foo").getJSONObject(0).remove("FIELDTOREMOVE");
        System.out.println("Value after edit: " + jsonObject.toString());

    }

}

The output would be:

Value before edit: {"foo":[{"FIELDTOREMOVE":0,"ref":"abcd","description":"dflt desc","customArray":{"number":22,"letter":"B"}}],"moreInfo":{"name":"Jhon Doe","email":"test@test.be"}}
Value after edit: {"foo":[{"ref":"abcd","description":"dflt desc","customArray":{"number":22,"letter":"B"}}],"moreInfo":{"name":"Jhon Doe","email":"test@test.be"}}

So you can manipulate with your JSON content and then post is as a String.

Related