'Java code to Convert JSON into a Map with specific fields as Key and values
I have a JSON as follows
[
{
"a": "John",
"id": "6",
"c": "val1"
},
{
"a": "Jack",
"id": "6",
"c": "val2"
},
{
"a": "Joe",
"id": "6",
"c": "val3"
}
]
I need to convert it into a Map<String, String> such that the values of the fields 'a' become the key and the values of the fields 'c' become the value in the Map.
In other words, my Map should look like the below:
John:val1
Jack:val2
Joe:val3
What is the shortest way to do this?
Also, I was wondering if in any way RestAssured GPath can be leveraged here
Something like this -
new JsonPath(jsonPayload).getString("findAll { json -> json.id == '6' }.a");
Solution 1:[1]
You can use jackson
for example:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.2.2</version>
</dependency>
I would create a wrapper for the resulting Map
and a custom deserializer.
@JsonDeserialize(using = MapWrapperDeserializer.class)
public class MapWrapper {
private final Map<String, String> map;
public MapWrapper(Map<String, String> map) {
this.map = map;
}
public Map<String, String> getMap() {
return this.map;
}
}
The deserializer:
public class MapWrapperDeserializer extends StdDeserializer<MapWrapper> {
public MapWrapperDeserializer() {
super(MapWrapper.class);
}
@Override
public MapWrapper deserialize(JsonParser parser, DeserializationContext context) throws IOException {
JsonNode array = parser.getCodec().readTree(parser);
int size = array.size();
Map<String, String> map = new LinkedHashMap<>(size);
for (JsonNode element : array) {
String key = element.get("a").asText();
String value = element.get("c").asText();
map.put(key, value);
}
return new MapWrapper(map);
}
}
A simple test:
public class Temp {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
InputStream dataStream = getInputStreamOrJsonString();
MapWrapper wrapper = mapper.readValue(dataStream, MapWrapper.class);
System.out.println(wrapper.getMap());
}
}
Solution 2:[2]
Are u looking for JsonSlurper ?
import groovy.json.JsonSlurper
String json = '''
[
{
"a": "John",
"id": "6",
"c": "val1"
},
{
"a": "Jack",
"id": "6",
"c": "val2"
},
{
"a": "Joe",
"id": "6",
"c": "val3"
}
]
'''
def root = new JsonSlurper().parseText(json)
def result = root.findAll{it.id == '6'}.collectEntries{[it.a, it.c]}
print(result)
Solution 3:[3]
You can use jsonpath
library to make it. Add it into your pom.xml:
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.4.0</version>
</dependency>
Then try following code
// read the a, c of json string by the JsonPath libraby
List<String> aList = JsonPath.read(json, "$.[*].a");
List<String> cList = JsonPath.read(json, "$.[*].c");
// combine two list to a map
Iterator<String> i1 = aList.iterator();
Iterator<String> i2 = cList.iterator();
Map<String, String> map = new HashMap<>();
while (i1.hasNext() && i2.hasNext()) {
map.put(i1.next(), i2.next());
}
// print it
map.forEach((k,v) -> System.out.println(k + ":" + v));
See more about jsonpath
Solution 4:[4]
You can convert it first to a list of YourObject and them convert it to a map following the rules you want ( key = a, value = c)
Create a class that represent the json object:
class YourObject
{
String a;
String id;
String c;
// contructors
// getters and setters
}
Desserialize your JSON into it using Gson:
String json = "<PUT YOUR JSON HERE>";
List<YourObject> list = new GsonBuilder().create().fromJson(json, new TypeToken<List<YourObject>>(){}.getType());
Then transform it to a map:
Map<String, String> map = list.stream().collect(Collectors.toMap(YourObject::getA, YourObject::getC));
If you want to filter by an specific id (forxample id=6) you can do like that:
Map<String, String> map = list.stream()
.filter(yo -> yo.getId().equals("6"))
.collect(Collectors.toMap(YourObject::getA, YourObject::getC));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Chaosfire |
Solution 2 | Bhanuchander Udhayakumar |
Solution 3 | Kai-Sheng Yang |
Solution 4 |