Mr. Kraus
Mr. Kraus

Reputation: 8135

How to find value in string array using JSONPath?

I am trying to filter a list of formParagraph objects where the hasContent property is true and the legalStatutes string array contains a string with 'AIA'. issue is that 'AIA' is not always in zero position in the array.

The below JSONPath only works if 'AIA' is first item in array.

   $.businessResponse.formParagraphList[ ? ((@.hasContent == true) && (@.legalStatutes[0] == 'AIA'))] {
    "rbacUser": null,
    "businessResponse": {
        "formParagraphList": [{
                "id": 1261,
                "hasContent": true,
                "legalStatutes": [
                    "Pre_AIA",
                    "AIA"
                ]
            },
            {
                "id": 67,
                "hasContent": true,
                "legalStatutes": [
                    "AIA",
                    "Pre_AIA"
                ]
            },
            {
                "id": 68,
                "hasContent": true,
                "legalStatutes": [
                    "Pre_AIA"
                ]
            }
        ]
    }
}

Is there a way to check for the existence of a specific string in a string array within a JSON object without knowing it's index using JSONPath?

Upvotes: 1

Views: 3245

Answers (1)

Mr. Kraus
Mr. Kraus

Reputation: 8135

Ok, figured out a solution to my problem. Used the .indexOf() method to check if the string exists in the array.

$.businessResponse.formParagraphList[?((@.hasContent==true) && (@.legalStatutes.indexOf('AIA') > -1))]

UPDATE: The above JSONPath is valid in online evaluators, but it didn't work in Newtonsoft for C#. here is what I got to work with this library.

$.businessResponse.formParagraphList[?(@.hasExaminerNote==true && @.legalStatutes[*]=='Pre_AIA')]

Upvotes: 2

Related Questions