Skip to content

ExprStringNameSpace.json_extract

Similar polars.Expr.str.json_extract, however you can only extract one field! You must specify the field. If the field is not in the json, the value will be null.

Parameters:

Name Type Description Default
field str

The field to extract from the json

required

Examples:

>>> qc = QuokkaContext()
>>> a = polars.from_dict({"a":[1,1,2,2], "c": ["A","B","C","D"], "b":['{"my_field": "quack"}','{"my_field": "quack"}','{"my_field": "quack"}','{"my_field": "quack"}']})
>>> a = qc.from_polars(a)
>>> a.with_columns({"my_field": a["b"].str.json_extract("my_field")}).collect()
Source code in pyquokka/expression.py
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
def json_extract(self, field):

    """
    Similar polars.Expr.str.json_extract, however you can only extract one field! You must specify the field.
    If the field is not in the json, the value will be null.

    Args:
        field (str): The field to extract from the json

    Examples:

        >>> qc = QuokkaContext()
        >>> a = polars.from_dict({"a":[1,1,2,2], "c": ["A","B","C","D"], "b":['{"my_field": "quack"}','{"my_field": "quack"}','{"my_field": "quack"}','{"my_field": "quack"}']})
        >>> a = qc.from_polars(a)
        >>> a.with_columns({"my_field": a["b"].str.json_extract("my_field")}).collect()

    """
    assert type(self.expr.sqlglot_expr.expression) == sqlglot.exp.Column, "json_extract can only be applied to an untransformed column"
    col_name = self.expr.sqlglot_expr.expression.name
    return Expression(sqlglot.dataframe.sql.Column(sqlglot.parse_one("json_extract_string({}, '{}')".format(col_name, field))))