0
{
  "dataObject": [
    {
      "id": 263626,
      "role": {
        "id": 12054,
        "name": "Edit",
        "description": ""
      },
      "resource": {
        "id": 5756,
        "type": "resource"
      }
    },
    {
      "id": 263364,
      "role": {
        "id": 12054,
        "name": "Edit",
        "description": ""
      },
      "resource": {
        "id": 5728,
        "type": "resource"
      }
    }
  ]
}

I have a JSON object which looks like this. I need to extract the json object from dataObject which has name:Edit and id:5756. How can I achieve this using JSON path? Tried $..[?(@.name="Edit", @.id=5756)] which didn't work.

Java code:

JsonPath.parse(json).read("$..[?(@.name='Edit'), (@.id=5756)]")
7
  • Please show the java code that you have tried. Commented Sep 18, 2019 at 5:18
  • Updated the question Commented Sep 18, 2019 at 5:23
  • Maybe it's because attribute name="Edit" in object role, but id=5756 in object resource, so you trying to point two different objects? Commented Sep 18, 2019 at 5:31
  • Yes. they are part of 2 objects which are part of one object in the array. Commented Sep 18, 2019 at 5:32
  • Try $..[?((@.resource.id == 5756) && (@.role.name == 'Edit'))] In my unit test seems like working. Commented Sep 18, 2019 at 6:26

1 Answer 1

1

Try to use parent object name and logical operator AND. The resulting path will be

$..[?((@.resource.id == 5756) && (@.role.name == 'Edit'))].

It's work for me in unit test with com.jayway.jsonpath:json-path:2.4.0

@Test
public void testParse() {
    String json = "{\n" +
            "  \"dataObject\": [\n" +
            "    {\n" +
            "      \"id\": 263626,\n" +
            "      \"role\": {\n" +
            "        \"id\": 12054,\n" +
            "        \"name\": \"Edit\",\n" +
            "        \"description\": \"\"\n" +
            "      },\n" +
            "      \"resource\": {\n" +
            "        \"id\": 5756,\n" +
            "        \"type\": \"resource\"\n" +
            "      }\n" +
            "    },\n" +
            "    {\n" +
            "      \"id\": 263364,\n" +
            "      \"role\": {\n" +
            "        \"id\": 12054,\n" +
            "        \"name\": \"Edit\",\n" +
            "        \"description\": \"\"\n" +
            "      },\n" +
            "      \"resource\": {\n" +
            "        \"id\": 5728,\n" +
            "        \"type\": \"resource\"\n" +
            "      }\n" +
            "    }\n" +
            "  ]\n" +
            "}";

    DocumentContext parse = JsonPath.parse(json);
    Object read = parse.read("$..[?((@.resource.id == 5756) && (@.role.name == 'Edit'))]");
    assertNotNull(read);
}
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.