0

I have below json response. Below response for layer2 object array there can be x number of items

{"data": {
        "layer1": {
            "layer2": [
                {
                    "item1": "result1",
                    "item2": "result2"
                },
                {
                    "item1": "result3",
                    "item2": "result4"
                }
                ]
            }
        }
}

My requirement is if I know value of one element (e.g. item1 value result4), How do I get the correspondence item value of item1 which is result3.

I have the below code where I can retrieve the object array. Is that possible to retrieve above with below output.

List<Object> actual = response.jsonPath().getList("data.layer1.layer2");
3
  • Can you iterate actual and filter your required object? Commented May 5, 2020 at 7:46
  • Why did you use gridlayer1 when there is only layer1 object in your JSON? A typo? Commented May 5, 2020 at 7:48
  • @Fenio Sorry it was a typo. Corrected Commented May 5, 2020 at 8:14

1 Answer 1

1

I think you meant if item2 is result4 then find item1. With the code you have written you can iterate the list and typecast the object to map and check if item2 exists with value result4 then get item1.

for(Object item: actual)
{
     if(((Map)item).get("item2").equals("result4")){
        return ((Map)item).get("item1");
     } 
}

PS: I haven't tested this code but logically it should work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.