Search in sources :

Example 1 with JsonValue

use of org.hisp.dhis.jsontree.JsonValue in project dhis2-core by dhis2.

the class H2SqlFunction method jsonb_extract_path_text.

// Postgres inbuilt function
public static String jsonb_extract_path_text(PGobject json, String... paths) {
    try {
        String content = json.getValue();
        if (content == null) {
            return null;
        }
        JsonValue value = new JsonResponse(content).get(toJsonPath(paths));
        if (!value.exists()) {
            return null;
        }
        if (value.node().getType() == JsonNodeType.STRING) {
            return value.as(JsonString.class).string();
        }
        return value.node().getDeclaration();
    } catch (Exception e) {
        log.error("Failed to extract path", e);
        throw e;
    }
}
Also used : JsonValue(org.hisp.dhis.jsontree.JsonValue) JsonString(org.hisp.dhis.jsontree.JsonString) JsonString(org.hisp.dhis.jsontree.JsonString) JsonResponse(org.hisp.dhis.jsontree.JsonResponse) SQLException(java.sql.SQLException)

Example 2 with JsonValue

use of org.hisp.dhis.jsontree.JsonValue in project dhis2-core by dhis2.

the class H2SqlFunction method jsonb_extract_path.

// Postgres inbuilt function
public static String jsonb_extract_path(PGobject json, String... paths) throws SQLException {
    try {
        String content = json.getValue();
        if (content == null) {
            return null;
        }
        JsonValue value = new JsonResponse(content).get(toJsonPath(paths));
        if (!value.exists()) {
            return null;
        }
        return value.node().getDeclaration();
    } catch (Exception e) {
        log.error("Failed to extract path", e);
        throw e;
    }
}
Also used : JsonValue(org.hisp.dhis.jsontree.JsonValue) JsonString(org.hisp.dhis.jsontree.JsonString) JsonResponse(org.hisp.dhis.jsontree.JsonResponse) SQLException(java.sql.SQLException)

Aggregations

SQLException (java.sql.SQLException)2 JsonResponse (org.hisp.dhis.jsontree.JsonResponse)2 JsonString (org.hisp.dhis.jsontree.JsonString)2 JsonValue (org.hisp.dhis.jsontree.JsonValue)2