Saturday, April 4, 2015

Filtering JSON – A Simple Way

 

Lets say you have some JSON data coming from some application and you want only some of it should go outside,filtering sensitive or irrelevant parts of it.

So overall you want :-

Input JSON  ->  Filtering Logic  -> Output JSON

Now how to do this? Well there is simple & fast library called minimal-json.

 

Input JSON Structure:

input.json

 

Output JSON Structure :

output.json

So from above pictures you can see we need to filter input json & restructure it to form an output json.

Steps:

1) Create a maven project & add minimal-json dependency to it.

2) Now read Input json to string

    String input = new Scanner(new File("input.json")).useDelimiter("\\Z").next();

3) Now create JSON parser from it.

    JsonObject injsonobj = JsonObject.readFrom(input);

4) JsonObject outjson = new JsonObject(); as output json object

5) You can easily add json nodes to output json object which is outjson here

     eg. outjson.add("totalcount", injsonobj.get("hits").asObject().get("total").asInt());
           outjson.add("perpage",20);

6) Now library is very effective for parsing JSON nodes as Java Datatypes. Inorder to get JSON nodes it provides get(“name of node”) method which returns JsonObject as ouput,this JsonObject can be parsed as Java Datatype by asDouble(),asInteger().

7) If you want to get JSON Array there is a method called asArray() which returns JsonArray object. From this object you can get individual object from get(index_number) method

JsonArray jhits2 = jhits.get("hits").asArray();
       
        for(int i=0;i<jhits2.size();i++)
        {
            JsonObject source = jhits2.get(i).asObject();
            double score = source.get("_score").asDouble();
            source = source.get("_source").asObject();
            JsonValue prod_details = source.get("productdetails");
            prod_details = ((JsonObject)prod_details).add("score", score);
            outprodarray.add(prod_details);
        }

8) Now at end when full filtering & restructuring done, you can now produce JSON output as outjson.toString()

To produce output from input of ~30KB it took less than 10 ms.(average 7ms) on 2GHz dual core machine. Well that means system can serve ~100 requests per second.