Skip to content
Advertisement

Java code to Convert JSON into a Map with specific fields as Key and values

I have a JSON as follows

JavaScript

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:

JavaScript

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 –

JavaScript

Advertisement

Answer

You can use jackson for example:

JavaScript

I would create a wrapper for the resulting Map and a custom deserializer.

JavaScript

The deserializer:

JavaScript

A simple test:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement