Home > Enterprise >  JSON object is getting removed with remove method in JSON file instead of JSON array
JSON object is getting removed with remove method in JSON file instead of JSON array

Time:07-03

I am trying to remove JSON array from a JSON file using org.json library

I am trying to remove webAutomation JSON array from the JSON file as follows

{
    "instructor": "Test_Instructor",
    "url": "www.google.com",
    "services": "Test Automation Service",
    "expertise": "Testing",
    "linkedIn": "linkedIn",
    "courses": {
        "webAutomation": [
            {
                "price": "500",
                "courseTitle": "Selenium"
            },
            {
                "price": "333",
                "courseTitle": "Protractor"
            }
        ],
        "apiAutomation": [
            {
                "price": "344.00",
                "courseTitle": "Rest Assured API Automation"
            }
        ],
        "mobileAutomation": [
            {
                "price": "4555",
                "courseTitle": "Appium"
            }
        ]
    }
}

I tried following code. Here str has JSON file

JSONObject jsonObject = new JSONObject(str);
jsonObject.getJSONObject("courses").getJSONArray("webAutomation");
System.out.println("after removal");
String str2 = mapper.writeValueAsString(jsonObject);
System.out.println(str2);

This is removing the whole JSON object instead of just JSON Array. The output is {"empty":false} Please help

CodePudding user response:

You can use remove method in org.json.JSONObject#remove.

JSONObject json = new JSONObject(str);
json.getJSONObject("courses").remove("webAutomation");
System.out.println(json);

The output will be:

{
    "instructor": "Test_Instructor",
    "url": "www.google.com",
    "services": "Test Automation Service",
    "expertise": "Testing",
    "linkedIn": "linkedIn",
    "courses": {
        "apiAutomation": [
            {
                "price": "344.00",
                "courseTitle": "Rest Assured API Automation"
            }
        ],
        "mobileAutomation": [
            {
                "price": "4555",
                "courseTitle": "Appium"
            }
        ]
    }
}

CodePudding user response:

I think you mixed org.json and Jackson API. Please refer below solution using Jackson API.

Jackson Maven Dependency

       <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.13.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.13.3</version>
        </dependency>

Solution

    String str = "{ \"phoneNumbers\": [ { \"type\": \"home\", \"number\": \"212 555-1234\" } ], \"children\": [ \"Catherine\", \"Thomas\", \"Trevor\" ], \"spouse\": null }";
    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode root = objectMapper.readTree(str);
    List<JsonNode> nodeList = root.findParents("phoneNumbers");
    for (JsonNode node : nodeList) {
        if(node.findValue("phoneNumbers").isArray()) {
            ObjectNode objNode = (ObjectNode) node;
            objNode.remove("phoneNumbers");
            System.out.println("Removed Successfully!!");
        }
    }
    String result = objectMapper.writeValueAsString(root);
    System.out.println("-----------------------");
    System.out.println(result);
  • Related