0

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.

JaSON
  • 4,843
  • 2
  • 8
  • 15
user13906258
  • 161
  • 1
  • 13

2 Answers2

0
  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)]
Akshay G
  • 2,070
  • 1
  • 15
  • 33
0

Here click for git repo is the code to check JSON object is present in whole JSON object:

private static Map<String, Object> getJsonMapFlatten(String json) throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    MapType type = mapper.getTypeFactory().constructMapType(Map.class, String.class, Object.class);
    Map<String, Object> map = mapper.readValue(json, type);
    return JsonMapFlattener.flatten(map);
}

public static void getJsonValue(String json, String keyName) throws JsonProcessingException {
    Map<String, Object> map = getJsonMapFlatten(json);
    Iterator<Map.Entry<String, Object>> itr = map.entrySet().iterator();
    while (itr.hasNext()) {
        Map.Entry<String, Object> obj = itr.next();
        if (obj.getKey().contains(keyName)) {
            System.out.println(obj.getKey() + "==>" + obj.getValue());
        }
    }
}

public static void validateJson(String json, String jsonToCompare) throws JsonProcessingException {
    Map<String, Object> flattenJsonResponse = getJsonMapFlatten(json);
    Map<String, Object> flattenResponseToCompare = getJsonMapFlatten(jsonToCompare);
    List<String> jsonResponsePaths = getJsonPaths(flattenJsonResponse);
    List<String> responsePathsToCompare = getJsonPaths(flattenResponseToCompare);
    if (flattenJsonResponse.size() >= flattenResponseToCompare.size()) {
        for (String jsonPath : responsePathsToCompare) {
            if (isPathPresent(jsonResponsePaths, jsonPath)) {
                System.out.println("path present: " + jsonPath);
                System.out.println(getPath(jsonResponsePaths, jsonPath));
                System.out.println(flattenResponseToCompare.get(jsonPath) + "<==>" + flattenJsonResponse.get(getPath(jsonResponsePaths, jsonPath)));//store the values
            } else {
                System.out.println("path is not present: " + jsonPath); 
            }
        }
    } else {
        for (String jsonPath : jsonResponsePaths) {
            if (isPathPresent(responsePathsToCompare, jsonPath)) {
                System.out.println(flattenJsonResponse.get(jsonPath) + "==" + flattenResponseToCompare.get(getPath(responsePathsToCompare, jsonPath)));
            } else {
               
                System.out.println("path is not present: " + jsonPath);
            }
        }
    }
}

private static List<String> getJsonPaths(Map<String, Object> map) {
    List<String> list = new ArrayList<>();
    Iterator<String> itr = map.keySet().iterator();
    while (itr.hasNext()) {
        list.add(itr.next());
    }
    return list;
}

private static boolean isPathPresent(List<String> paths, String path) {
    for (String list : paths) {
        if (list.contains(path)) {
            return true;
        }
    }
    return false;
}

private static String getPath(List<String> paths, String path) {
    for (String list : paths) {
        if (list.contains(path)) {
            return list;
        }
    }
    throw new NullPointerException("No such path exist " + path);
}