3

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!

1

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

Comments

3

If you are using Jayway JsonPath you can use filter operator in

$..[?('loks3123' in @.plans[*].plan.planId)].skuId

Try here : https://jsonpath.herokuapp.com/


If you are using Newtonsoft.json

$..[?(@.plans[*].plan.planId == 'loks3123')].skuId

Try here : https://dotnetfiddle.net/6n4vAd

5 Comments

I'm using Newtonsoft.json, so this seems not work.
@peterChar if you are using newtonsoft the marked answer shouldn't work either.
@peterChar Check the fiddle using newtonsoft.json
I do use Jayway JSONPath and your answer seems so much easier to understand than mine. I learned something today. Thanks very much!
@Akshay G That works, Thank you! And sorry for my lately accept.
0

Here is a simple implementation for your problem using javascript and in the solution 'data' is your json data

    let findstring="loks3123";
    for(var i=0;i<data.length;i++){
        let sku=data[i];
        for(var j=0;j<sku.plans.length;j++){
            let plans=sku.plans[j];
            if(plans.plan.planId==findstring){
                console.log("skuId:"+sku.skuId);
            }
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.