Consider the following dataset:
[
{
"_id": 1234,
"bagnumber": "A",
"bagtype": "coin"
},
{
"_id": 2345,
"bagnumber": "B",
"bagtype": "cash"
},
{
"_id": 3456,
"bagnumber": "C",
"bagtype": "coin"
},
{
"_id": 5678,
"bagnumber": "D",
"bagtype": "cash"
}
]
I need to extract and print the bag numbers that have the bag type "cash" exclusively. Thus, the desired output should be:
Bag numbers are: B, D
I attempted to extract the bag numbers first using a list:
List<String> bagNumbers = response.jsonPath().getList("bagnumber");
Subsequently, I tried to retrieve the bag types using the same list and attempted to compare the two lists. However, my attempts were unsuccessful, possibly indicating an overly complicated approach.
Could someone kindly suggest a more efficient solution? Perhaps utilizing the Matchers class or any other suitable approach would be appreciated.
2
Answers
GSON
I suggest Gson or Jackson for Json process.
If the input are string, convert it to JsonArray using:
Create a List containing result:
Start iterating through array:
And the result is:
You may try JSON library Josson and your request can be done by short statement and functions.
https://github.com/octomix/josson