0

Looking to filter this json body for specific key/values for when a certain condition is met.

For this body - I'd like to retrieve ONLY the recipient ID and Tracking Number for when the requester ID is 67890.

{
    "metadata": "someinformation",
    "access": "XXXX",
    "recipient": {
        "id": "12345"
    },
    "requester": {
        "id": "67890"
    },
    "trackingNumber": "ABCDEF"
}

This would be using Goessner https://goessner.net/articles/JsonPath/index.html

I am able to get the attributes mostly using: $..[trackingNumber,requester,recipient] but it removes the key of "trackingNumber" and only does a value.

Also the filter I want to use alongside that would be: [?($.requester.id=="67890")]

The expectation is other requester ID's will be in other json bodies - but we only want to filter for the ones that have this present and select the specific attributes.

  • Is this payload a single item in an array of items that look like this? Currently you just have a single object, but your question suggests you have multiple. – gregsdennis Mar 25 '22 at 03:11

1 Answers1

0

You going to need to do this in two queries, one for each value that you want back.

For recipient:

$[?(@.requester.id == '67890')].recipient.id

For tracking number:

$[?(@.requester.id == '67890')].trackingNumber

I don't think Goessner's implementation supports returning multiple values like you want. It's not something that will be supported in the upcoming spec, either.

gregsdennis
  • 7,218
  • 3
  • 38
  • 71