JSON

JSONPath – JSONPath Query on JSON Array with Search Criteria

JSONPath – JSONPath Query on JSON Array with Search Criteria

   JSON Array Format ( Source Input )
 
   [
   {
      "id" : 1,
      "equipment" : "Switch",
      "model" : "Cisco Catalyst 2960",
      "sn" : "XXXXXXXXXX",
      "location" : "XXXXXXXXXX",
      "building" : "XXXXXXXXXX",
      "rack" : "",
      "startunit" : 0,
      "endunit" : 0,
      "status" : "In Service",
      "produatdev" : "UAT",
      "po" : "",
      "podate" : null,
      "maintemplate" : "M9",
      "vendor" : "XXXXXXXXXX",
      "remarks" : "",
      "mainstartdate" : "2011-01-01",
      "mainenddate" : "2012-01-01",
      "itemsystem" : "Switch"
   }, {
      "id" : 2,
      "equipment" : "Switch",
      "model" : "Cisco Catalyst 2960",
      "sn" : "XXXXXXXXXX",
      "location" : "XXXXXXXXXX",
      "building" : "XXXXXXXXXX",
      "rack" : "",
      "startunit" : 0,
      "endunit" : 0,
      "status" : "In Service",
      "produatdev" : "Production",
      "po" : "",
      "podate" : null,
      "maintemplate" : "M9",
      "vendor" : "XXXXXXXXXX",
      "remarks" : "",
      "mainstartdate" : "2010-01-01",
      "mainenddate" : "2011-01-01",
      "itemsystem" : "Switch"
   }, {
   … …
   ]
 
   JSONPath Query and "com.jayway.jsonpath.JsonPath" Library – Usage Code
 
   $[?(@.itemsystem == 'Switch' || @.itemsystem == 'Router')]
 
 
   import com.jayway.jsonpath.JsonPath;
 
   … …
 
   String JSONPathQuery = "$[?(@.itemsystem == 'Switch' || @.itemsystem == 'Router')]";
 
   List<HashMap<String, Object>> result = dao.getInventoryListByJSONPath(JSONArrayStr, JSONPathQuery);
 
   System.out.println(result.size());
 
   for(HashMap<String, Object> obj : result)
   {
         for(String item : obj.keySet())
         {
            if(item.equals("itemsystem")) System.out.println(item + " : " + obj.get(item));
            if(item.equals("model")) System.out.println(item + " : " + obj.get(item));
         }
 
         System.out.println("");
   }
 

More Development Sample on "com.jayway.jsonpath.JsonPath" Library.