How to check the input string present in json array using json path

Viewed 38

I am trying to check the given input string is present in a json array. Below is my sample json

{
    "school": {
        "class": {
            "standard": "6",
            "student_list": [
                "Daniel",
                "Jack",
                "John"
            ]
        }
    }
}

Let say i am trying to find a given input name is part of student_list if the given input name matches then it should retrieve the complete details of the class. Let say i am passing name 'John' it should give me below result

{
            "standard": "6",
            "student_list": [
                "daniel",
                "jack",
                "john"
            ]
        }

I am using jayway json path library i tried couple of ways like below

$.school.class.[?('john' anyof @.student_list)]
$.school.class.[?(@.'john' anyof @.student_list)]

But every time it is giving me empty array. I am new to jsonpath query could you please point me where i am going wrong or help me what is wrong with my json path query.

1 Answers
  1. The comparison is case sensitive.
  2. Filter Operator anyof -- left has an intersection with right [?(@.sizes anyof ['M', 'L'])]
  3. Indefinite paths always returns a list

Change your query to

$.school.class[?(@.student_list anyof ['John'])]

OR

$.school.class[?(@.student_list contains 'John')]

OR

$.school.class[?('John' in @.student_list)]
Related