0

Im trying to get JSONPath expression to filter my JSON and get whole sport object using value of child array.

I have following JSON:

[{

        "name": "Soccer",
        "regions": [{
                "name": "Australia",
                "leagues": [{
                        "name": "Australia league",
                        "inplay": 5,
                    }
                ]
            }
        ]
    }, {

        "name": "Tennis",
        "regions": [{
                "name": "Germany",
                "leagues": [{
                        "name": "Germany league",
                        "inplay": 0,
                    }
                ]
            }
        ]
    }
]

I need to get whole sport object where "inplay == 0" using JsonPath expression.

Result should look like that:

 {

    "name": "Tennis",
    "regions": [{
            "name": "Germany",
            "leagues": [{
                    "name": "Germany league",
                    "inplay": 0,
                }
            ]
        }
    ]
}

Regions and Leagues count can be > 1 Therefore $[?(@.regions[0].leagues[0].inplay == 0)] is not suitable

Tried $[?(@.regions[*].leagues[*].inplay == 0)] but it doesnt work

2 Answers 2

1

Since this is not directly supported (as of now) in JayWay JSONPath we leverage contains as a workaround:

$[?(@.regions..inplay contains '0')]

Note: It may look like contains would work similar to a 'like' operator or instr function but this is not the case here. If the inplay value contains a 0, e.g. 10 it would not pull the record (according to my tests;)

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

Comments

0

This works for me

$[?(@.regions[0].leagues[0].inplay == 0)]

2 Comments

Thanks for answer. But regions and leagues can be more than 1. Tried $[?(@.regions[*].leagues[*].inplay == 0)] but it doesnt work
Yes, deep scan or wild card cannot be used in expression. I think you need to find another way, I run out of idea right now.

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.