Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSONPath : How to filter nested array?

Tags:

json

jsonpath

I have a json like below:

[
    {
        "skuId": "1234",
        "plans": [
            {
                "plan": {
                    "planName": "plan1",
                    "planId": "abcd1231",
                }
            },
            {
                "plan": {
                    "planName": "plan2",
                    "planId": "loks3123",
                }
            }
        ]
    },
    {
        "skuId": "5341",
        "plans": [
            {
                "plan": {
                    "planName": "plan3",
                    "planId": "awer3234",
                }
            },
            {
                "plan": {
                    "planName": "plan4",
                    "planId": "gefd4231",
                }
            }
        ]
    },
    {
        "skuId": "7649",
        "plans": [
            {
                "plan": {
                    "planName": "plan5",
                    "planId": "kitv5397",
                }
            }
        ]
    }
]

Now i have a planId "loks3123", and i want to get the skuId where it belongs to. In this case planId "loks3123" belongs to skuId "1234". Is it possible using JsonPath to do that? If so, How to do that using JsonPath? If not, what should i do? Thanks!

like image 735
peter Char Avatar asked Dec 02 '25 17:12

peter Char


1 Answers

You need to find a node containing a plans attribute that contains a planId attribute that matches your text. Using the suggestion at the end of https://github.com/json-path/JsonPath/issues/287 you can get what you want with:

$..[?(@.plans[?(@.plan.planId == 'abcd1231')] empty false)].skuId

like image 83
Jerry Jeremiah Avatar answered Dec 07 '25 10:12

Jerry Jeremiah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!