Deepak
Deepak

Reputation: 51

JsonPath expression for Json String?

I have below JSON String. I need some help with writing a Jayway JsonPath expression that gets assignees for a specific role, say "Editor":

{
  "assignments": [
    {
      "role": {
        "label": "Editor",
        "id": "8bc3e27aafe310001fc933216b890910"
      },
      "assignees": [
        {
          "label": "user 1",
          "id": "bd76276f5e5344e4b27f8600c9000726"
        },
        {
          "label": "user 2",
          "id": "abbb210550af1017329208ee8632618c"
        }
      ]
    },
    {
      "role": {
        "label": "Owner",
        "id": "8bc3e27aafe310001fc932f0d928090f"
      },
      "assignees": [
        {
          "label": "user 3",
          "id": "3db5d9e1081e470bb98d5a3d15b67180"
        },
        {
          "label": "user 4",
          "id": "586e403c186c01521f763b5d4d0e6000"
        }
      ]
    },
    {
      "role": {
        "label": "Viewer",
        "id": "8bc3e27aafe310001fc9333ecccf0911"
      },
      "assignees": [
        {
          "label": "user 5",
          "id": "9bdfa0b60668100b619f393cfc282340"
        },
        {
          "label": "user 6",
          "id": "586e403c186c018f6c8d798c4e0e6a00"
        }
      ]
    }
  ]
}

Upvotes: 0

Views: 398

Answers (2)

Deepak
Deepak

Reputation: 51

I figured the answer

$.assignments[?(@.role.id=='8bc3e27aafe310001fc933216b890910')].assignees

Upvotes: 0

Michael Kay
Michael Kay

Reputation: 163645

Assuming you're using XPath 3.1 which is the first version to provide XPath access to JSON data, you can write

assignments[?role?label = 'Editor']?assignees

Upvotes: 1

Related Questions