Skip to content

Commit 1377b7a

Browse files
BAEL-9214: conversion from json object to json array (#18672)
Co-authored-by: sverma1-godaddy <[email protected]>
1 parent a6f4c06 commit 1377b7a

File tree

6 files changed

+148
-0
lines changed

6 files changed

+148
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.jsonobjecttojsonarray;
2+
3+
import com.google.gson.JsonArray;
4+
import com.google.gson.JsonObject;
5+
import org.json.JSONObject;
6+
7+
public class GsonConverter {
8+
9+
JsonArray convertToKeyValueArray(JSONObject jsonObject) {
10+
JsonArray result = new JsonArray();
11+
jsonObject.keySet().forEach(key -> {
12+
JsonObject entry = new JsonObject();
13+
entry.addProperty("key", key);
14+
entry.add("value", com.google.gson.JsonParser.parseString(jsonObject.get(key).toString()));
15+
result.add(entry);
16+
});
17+
return result;
18+
}
19+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.jsonobjecttojsonarray;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.fasterxml.jackson.databind.node.ArrayNode;
6+
import com.fasterxml.jackson.databind.node.ObjectNode;
7+
import org.json.JSONObject;
8+
9+
public class JacksonConverter {
10+
11+
ArrayNode convertToArray(JSONObject jsonObject) {
12+
ObjectMapper mapper = new ObjectMapper();
13+
JsonNode jsonNode = mapper.convertValue(jsonObject.toMap(), JsonNode.class);
14+
15+
ArrayNode result = mapper.createArrayNode();
16+
jsonNode.fields().forEachRemaining(entry -> {
17+
ObjectNode obj = mapper.createObjectNode();
18+
obj.put("key", entry.getKey());
19+
obj.set("value", entry.getValue());
20+
result.add(obj);
21+
});
22+
23+
return result;
24+
}
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.jsonobjecttojsonarray;
2+
3+
import org.json.JSONArray;
4+
import org.json.JSONObject;
5+
6+
public class OrgJsonConverter {
7+
8+
JSONArray convertValuesToArray(JSONObject jsonObject) {
9+
return new JSONArray(jsonObject.toMap().values());
10+
}
11+
12+
JSONArray convertToEntryArray(JSONObject jsonObject) {
13+
JSONArray result = new JSONArray();
14+
for (String key : jsonObject.keySet()) {
15+
JSONObject entry = new JSONObject();
16+
entry.put("key", key);
17+
entry.put("value", jsonObject.get(key));
18+
result.put(entry);
19+
}
20+
return result;
21+
}
22+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.jsonobjecttojsonarray;
2+
3+
import com.google.gson.JsonArray;
4+
import org.json.JSONObject;
5+
import org.junit.jupiter.api.Test;
6+
import static org.junit.jupiter.api.Assertions.*;
7+
8+
class GsonConverterTest {
9+
10+
@Test
11+
void givenJSONObject_whenConvertToKeyValueArray_thenJsonArrayWithObjects() {
12+
JSONObject jsonObject = new JSONObject();
13+
jsonObject.put("brand", "Tesla");
14+
jsonObject.put("year", 2024);
15+
16+
GsonConverter converter = new GsonConverter();
17+
System.out.println("before :"+jsonObject);
18+
JsonArray result = converter.convertToKeyValueArray(jsonObject);
19+
20+
System.out.println("here :"+result);
21+
22+
assertEquals(2, result.size());
23+
assertEquals("year", result.get(0).getAsJsonObject().get("key").getAsString());
24+
}
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.jsonobjecttojsonarray;
2+
3+
import org.json.JSONObject;
4+
import com.fasterxml.jackson.databind.node.ArrayNode;
5+
import org.junit.jupiter.api.Test;
6+
import static org.junit.jupiter.api.Assertions.*;
7+
8+
class JacksonConverterTest {
9+
10+
@Test
11+
void givenJSONObject_whenConvertToArray_thenArrayNodeOfKeyValueObjects() {
12+
JSONObject jsonObject = new JSONObject();
13+
jsonObject.put("country", "India");
14+
jsonObject.put("code", "IN");
15+
16+
JacksonConverter converter = new JacksonConverter();
17+
ArrayNode result = converter.convertToArray(jsonObject);
18+
19+
assertEquals(2, result.size());
20+
assertEquals("country", result.get(0).get("key").asText());
21+
}
22+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.jsonobjecttojsonarray;
2+
3+
import org.json.JSONObject;
4+
import org.json.JSONArray;
5+
import org.junit.jupiter.api.Test;
6+
import static org.junit.jupiter.api.Assertions.*;
7+
8+
public class OrgJsonConverterTest {
9+
10+
@Test
11+
void givenFlatJSONObject_whenConvertValues_thenJSONArrayOfValues() {
12+
JSONObject jsonObject = new JSONObject();
13+
jsonObject.put("id", 1);
14+
jsonObject.put("name", "Alice");
15+
16+
OrgJsonConverter converter = new OrgJsonConverter();
17+
JSONArray result = converter.convertValuesToArray(jsonObject);
18+
19+
assertEquals(2, result.length());
20+
assertTrue(result.toList().contains("Alice"));
21+
}
22+
23+
@Test
24+
void givenFlatJSONObject_whenConvertToEntryArray_thenJSONArrayOfObjects() {
25+
JSONObject jsonObject = new JSONObject();
26+
jsonObject.put("language", "Java");
27+
jsonObject.put("framework", "Spring");
28+
29+
OrgJsonConverter converter = new OrgJsonConverter();
30+
JSONArray result = converter.convertToEntryArray(jsonObject);
31+
32+
assertEquals(2, result.length());
33+
assertEquals("framework", result.getJSONObject(0).get("key"));
34+
}
35+
}

0 commit comments

Comments
 (0)