pyflink.table.expression.Expression.json_value#
- Expression.json_value(path: str, returning_type: DataType = VarCharType(2147483647, true), on_empty: JsonValueOnEmptyOrError = JsonValueOnEmptyOrError.NULL, default_on_empty: Any | None = None, on_error: JsonValueOnEmptyOrError = JsonValueOnEmptyOrError.NULL, default_on_error: Any | None = None) Expression[source]#
Extracts a scalar from a JSON string.
This method searches a JSON string for a given path expression and returns the value if the value at that path is scalar. Non-scalar values cannot be returned. By default, the value is returned as DataTypes.STRING(). Using returningType a different type can be chosen, with the following types being supported:
STRING
BOOLEAN
INT
DOUBLE
For empty path expressions or errors a behavior can be defined to either return null, raise an error or return a defined default value instead.
See also
Examples:
>>> lit('{"a": true}').json_value('$.a') # STRING: 'true' >>> lit('{"a.b": [0.998,0.996]}').json_value("$.['a.b'][0]", DataTypes.DOUBLE()) # DOUBLE: 0.998 >>> lit('{"a": true}').json_value('$.a', DataTypes.BOOLEAN()) # BOOLEAN: True >>> lit('{"a": true}').json_value('lax $.b', JsonValueOnEmptyOrError.DEFAULT, False) # BOOLEAN: False >>> lit('{"a": true}').json_value('strict $.b', JsonValueOnEmptyOrError.NULL, None, JsonValueOnEmptyOrError.DEFAULT, False) # BOOLEAN: False
Added in version 1.12.0.