javajsonpathjayway

JsonPath (Jayway) efficient replacement


I am using Jsonpath in Java.

I have a simple json payload that can contain up to 100 different values. I have to systematically read 70 of these values. So I am iterating a list of the 70 paths and just do json.read("path"). And if I get a value if manage it. If null I skip it. This approach is slow with high volume of data and I was wondering if there was another to read these data?

Would it be more efficient to search the node first and then get the content?

Any idea?

I'd like to speed up the processing.

Many thanks in advance


Solution

  • One way to optimize it, is to precompile the JSON-Expression in your constructor once and reuse it on function call.

    You could e.g. save the compiled Expressions in a map and get them based on the value of the key:

    ...
    public YourClass(){
       jsonPathMap.put('someValue', JsonPath.compile("$path.to.your.someValue");
    ...
    }
    
    public void doTheParsing(){
     var myJSONString = '{...}'
     var someValue = this.jsonPathMap.get('someValue').read(myJSONString);
    ...
    }