简体   繁体   中英

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?

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')]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM