satchel
satchel

Reputation: 547

How to use JSONPath to filter JSON body with nested arrays

This is the JSON Path I have been using:

$..products[?(@.category == 'merchant_cards' && @.countries[0].abbr == 'US' && @.skus[0].min < '25')]

However, I am getting an error end of input expected at 1:12

Here is an excerpt of the body:

[
  {
    "id": "ACGZXBCIGX8Y",
    "name": "Overstock.com",
    "currency_codes": [
      "USD"
    ],
    "skus": [
      {
        "min": 5,
        "max": 500
      }
    ],
    "countries": [
      {
        "abbr": "US"
      }
    ],
    "category": "merchant_cards",
    "disclosure": "",
    "description": "",
    "images": [
      {
        "src": "https://giftrocket-s3.imgix.net/Brands/US/Overstock/Digital/Overstock.png",
        "type": "card"
      }
    ]
  },

I want to be able to filter on some nested lists and that seems to be causing the problems.

For example, I want only those in the US. countries is a list of one, which has a node of abbr which can have multiple values.

Upvotes: 0

Views: 1266

Answers (1)

Delta George
Delta George

Reputation: 4236

When I complete the json array, this works:

$[?(@.category == 'merchant_cards' && @.countries[0].abbr == 'US' && @.skus[0].min < 25)]

I also changed the min limit to an int.

Try it on https://jsonpath.com/

Upvotes: 1

Related Questions