Skip to content

JSONPath expression for nested arrays #591

@kshorty

Description

@kshorty

Hello!

I've tested this several ways and cannot extract the desired results when I have nested arrays in my JSON.

Sample JSON:

{
  "stores": [
    {
      "store": {
        "storeInfo": {
          "storeId": "111",
          "storeName": "Store Name 1",
          "storeDisplayName": null,
          "storePortfolioId": "10059595",
          "storeClass": "BookStore",
        },
        "bookInfo": {
          "bookPrice": "8.62",
          "bookAsOfDate": "1575849600000",
        },
        "storeClasses": [
          {
            "filterStoreClass": {
              "levelOneName": "Level One",
              "levelTwoName": "Level Two"
            }
          }
        ],
        "relatedBooks": [
          {
            "relatedBook": {
              "bookInfo": {
                "bookId": "121",
                "bookName": "My Related Book 1",
                "bookSymbol": "A",
                "bookProductCode": "0010",
                "status": "Open"
              },
              "currencyInfo": null,
              "otherRelatedInfo": {
                "indicator": false,
                "otherId": null,
                "otherDeadlines": [
                ]
              }
            }, 
            "relatedBook": {
              "bookInfo": {
                "bookId": "122",
                "bookName": "My Related Book 2",
                "bookSymbol": "AA",
                "bookProductCode": "0011",
                "status": "Open"
              },
              "currencyInfo": null,
              "otherRelatedInfo": {
                "indicator": false,
                "otherId": null,
                "otherDeadlines": [
                ]
              }
            }
          }
        ]
      }
    },
    {
      "store": {
        "storeInfo": {
          "storeId": "222",
          "storeName": "Store Name 2",
          "storeDisplayName": null,
          "storePortfolioId": "11595",
          "storeClass": "BookStore",
        },
        "bookInfo": {
          "bookPrice": "8.62",
          "bookAsOfDate": "1575849600000",
        },
        "storeClasses": [
          {
            "filterStoreClass": {
              "levelOneName": "Level One",
              "levelTwoName": "Level Two"
            }
          }
        ],
        "relatedBooks": [
          {
            "relatedBook": {
              "bookInfo": {
                "bookId": "122",
                "bookName": "My Related Book 3",
                "bookSymbol": "AAA",
                "bookProductCode": "0030",
                "status": "Open"
              },
              "currencyInfo": null,
              "otherRelatedInfo": {
                "indicator": false,
                "otherId": null,
                "otherDeadlines": [
                ]
              }
            }, 
            "relatedBook": {
              "bookInfo": {
                "bookId": "124",
                "bookName": "My Related Book 4",
                "bookSymbol": "AA",
                "bookProductCode": "0014",
                "status": "Open"
              },
              "currencyInfo": null,
              "otherRelatedInfo": {
                "indicator": false,
                "otherId": null,
                "otherDeadlines": [
                ]
              }
            }

          }
        ]
      }
    }      
  ]
}

There are 2 stores and each store has books with related books. If I find a related book in the store ideally I would like to return the storeId of that store (or the complete store object where there is a match). For example bookId=='122' is in both stores.

here is a breakdown of expressions I tried:

--- Returns the store information on found store id
$..store[?(@.storeInfo.storeId=='111')]

--- Returns just the found related book
$..relatedBooks[?(@.relatedBook.bookInfo.bookId == '122')]

--- Returns just the found related book
$..relatedBooks[?(@.relatedBook.bookInfo.bookProductCode == '0011')]

--- Returns the related book id
$..*[?(@.relatedBook.bookInfo.bookId == '122')]

--- NOT WORKING Returns the store with related book found
$..[?(@.store.relatedBooks[?(@.relatedBook.bookInfo.bookProductCode == '0010')])]'

--- NOT WORKING Returns bookId 122 for the first store and 124 for the second store
$..*[?(@.relatedBooks[?(@.relatedBook.bookInfo.bookId == '122')])]

Any help would be appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions