Revision 45633 trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/java/org/gvsig/expressionevaluator/impl/function/typeconversion/JsonValueFunction.java

View differences:

JsonValueFunction.java
22 22
 */
23 23
public class JsonValueFunction extends AbstractFunction {
24 24
    
25
    //JsonPathContextImpl implements JsonPathContext {
26
    // https://github.com/json-path/JsonPath
25
        //
26
        // function docs at:
27
        // Oracle json_value: https://docs.oracle.com/database/121/SQLRF/functions093.htm#SQLRF56668
28
        // SQLServer json_value: https://docs.microsoft.com/es-es/sql/t-sql/functions/json-value-transact-sql?view=sql-server-ver15
29
        // PostgreSQL jsonb_path_query: https://www.postgresql.org/docs/12/functions-json.html#FUNCTIONS-JSON-PROCESSING-TABLE
30
        // The implementation used is:
31
        // https://github.com/json-path/JsonPath
32
        //
33
    
27 34
    public JsonValueFunction() {
28 35
        super(GROUP_CONVERSION, FUNCTION_JSON_VALUE, Range.is(2),
29
                "Get value from a Json with a specific path.",
30
            FUNCTION_JSON_VALUE+"({{json}}, '[jsonpath]')");
31
        this.addAlias("JSON_VALUE");
36
                "finds a specified scalar JSON value in JSON data and returns it.",
37
            FUNCTION_JSON_VALUE+"({{expr}}, JsonPath)",
38
            new String[]{
39
                "expr - specify an expression that evaluates to a text literal.",
40
                "JsonPath - specify a JSON path expression."
41
            }
42
        );
32 43
    }
33 44

  
34 45
    @Override
......
38 49

  
39 50
    public static Object json_value(String json, String path) {
40 51
        JsonManager manager = ToolsUtilLocator.getJsonManager();
41
        JsonPathContext context = manager.createJSonPathContext(json);
52
        JsonPathContext context;
53
        try {
54
            context = manager.createJSonPathContext(json);
55
        } catch(Exception ex) {
56
            return null;
57
        }
42 58
        Object value = context.get(path);
43 59
        if( value instanceof List ) {
44 60
            List l = ((List)value);
......
56 72
        String path = this.getStr(args, 1);
57 73
        Object value = json_value(json, path);
58 74
        
59
        return value;   }
75
        return value;  
76
    }
60 77

  
61 78
}

Also available in: Unified diff