Reputation: 13
I have the next json file I want remove key and value for ProductCharacteristic ->name. How it can be done in Java?
[
{ "id": "0028167072_CO_FIX_INTTV_1008_P3909_IDX0",
"status": "Active",
"startDate": "2023-02-12T22:00:00Z",
"place": [ {
"id": "8",
"apartment": "578",
"role": "QA",
"@referredType": "street"
} ],
"ProductCharacteristic": [ {
"id": "CH_100473",
"valueId": "CH12_1000374_VALUE04141",
"value": "LTS",
"name": "Computer"
}
] }
]
Upvotes: 0
Views: 74
Reputation: 13
On building up on @Lauren answer. This code would work on JSONArrays(the sample provided has only one 1 JSONObject inside, this'll work even if there's more)
public static void main(String[] args) {
// Replace with your actual JSON string of load JSON from file
String jsonString = "[{\"id\":\"0028167072_CO_FIX_INTTV_1008_P3909_IDX0\",\"place\":[{\"@referredType\":\"street\",\"apartment\":\"578\",\"id\":\"8\",\"role\":\"QA\"}],\"ProductCharacteristic\":[{\"id\":\"CH_100473\",\"name\":\"Computer\",\"value\":\"LTS\",\"valueId\":\"CH12_1000374_VALUE04141\"}],\"startDate\":\"2023-02-12T22:00:00Z\",\"status\":\"Active\"}]";
JSONArray jsonArray = new JSONArray(jsonString);
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
JSONArray productCharacteristics = jsonObject.getJSONArray("ProductCharacteristic");
// Iterate through each "ProductCharacteristic" object and remove the "name"
// key-value pair
for (int j = 0; j < productCharacteristics.length(); j++) {
JSONObject productCharacteristic = productCharacteristics.getJSONObject(i);
productCharacteristic.remove("name");
}
}
String updatedJsonString = jsonArray.toString();
System.out.println("Modified JSON:\n" + updatedJsonString);
}
Although this works for JSONs which you have copied from the file. Use an input stream to read JSONs from the file. If an single valid JSON spans multiple lines, try and use an JSON streaming parser(I'd recommend Jackson) else the org.json's JSON library is enough.
Upvotes: 0
Reputation: 456
Assuming you have a proper java project set up with Maven, and the following dependency in your pom.xml
file:
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20230618</version>
</dependency>
You can parse and edit your JSON as follow:
public static void main(String[] args) {
// Replace with your actual JSON string of load JSON from file
String jsonString = "{ \"id\": \"0028167072_CO_FIX_INTTV_1008_P3909_IDX0\", \"status\": \"Active\", \"startDate\": \"2023-02-12T22:00:00Z\", \"place\": [ { \"id\": \"8\", \"apartment\": \"578\", \"role\": \"QA\", \"@referredType\": \"street\" } ], \"ProductCharacteristic\": [ { \"id\": \"CH_100473\", \"valueId\": \"CH12_1000374_VALUE04141\", \"value\": \"LTS\", \"name\": \"Computer\" } ] }";
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray productCharacteristics = jsonObject.getJSONArray("ProductCharacteristic");
// Iterate through each "ProductCharacteristic" object and remove the "name"
// key-value pair
for (int i = 0; i < productCharacteristics.length(); i++) {
JSONObject productCharacteristic = productCharacteristics.getJSONObject(i);
productCharacteristic.remove("name");
}
String updatedJsonString = jsonObject.toString();
System.out.println("Modified JSON:\n" + updatedJsonString);
}
Upvotes: 1