We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 090ce8e commit 0fc795aCopy full SHA for 0fc795a
bigframes/bigquery/__init__.py
@@ -51,6 +51,7 @@
51
json_value,
52
json_value_array,
53
parse_json,
54
+ to_json,
55
to_json_string,
56
)
57
from bigframes.bigquery._operations.search import create_vector_index, vector_search
@@ -89,6 +90,7 @@
89
90
91
92
93
94
95
# search ops
96
create_vector_index,
bigframes/bigquery/_operations/json.py
@@ -430,6 +430,40 @@ def json_value_array(
430
return input._apply_unary_op(ops.JSONValueArray(json_path=json_path))
431
432
433
+def to_json(
434
+ input: series.Series,
435
+) -> series.Series:
436
+ """Converts a series with a JSON value to a JSON-formatted STRING value.
437
+
438
+ **Examples:**
439
440
+ >>> import bigframes.pandas as bpd
441
+ >>> import bigframes.bigquery as bbq
442
+ >>> bpd.options.display.progress_bar = None
443
444
+ >>> s = bpd.Series([1, 2, 3])
445
+ >>> bbq.to_json(s)
446
+ 0 1
447
+ 1 2
448
+ 2 3
449
+ dtype: extension<dbjson<JSONArrowType>>[pyarrow]
450
451
+ >>> s = bpd.Series([{"int": 1, "str": "pandas"}, {"int": 2, "str": "numpy"}])
452
453
+ 0 {"int":1,"str":"pandas"}
454
+ 1 {"int":2,"str":"numpy"}
455
456
457
+ Args:
458
+ input (bigframes.series.Series):
459
+ The Series containing JSON or JSON-formatted string values.
460
461
+ Returns:
462
+ bigframes.series.Series: A new Series with the JSON value.
463
+ """
464
+ return input._apply_unary_op(ops.ToJSON())
465
466
467
def to_json_string(
468
input: series.Series,
469
) -> series.Series:
bigframes/core/compile/ibis_compiler/scalar_op_registry.py
@@ -1302,6 +1302,11 @@ def parse_json_op_impl(x: ibis_types.Value, op: ops.ParseJSON):
1302
return parse_json(json_str=x)
1303
1304
1305
+@scalar_op_compiler.register_unary_op(ops.ToJSON)
1306
+def to_json_op_impl(json_obj: ibis_types.Value):
1307
+ return to_json(json_obj=json_obj)
1308
1309
1310
@scalar_op_compiler.register_unary_op(ops.ToJSONString)
1311
def to_json_string_op_impl(x: ibis_types.Value):
1312
return to_json_string(value=x)
@@ -2093,6 +2098,11 @@ def json_extract_string_array( # type: ignore[empty-body]
2093
2098
"""Extracts a JSON array and converts it to a SQL ARRAY of STRINGs."""
2094
2099
2095
2100
2101
+@ibis_udf.scalar.builtin(name="to_json")
2102
+def to_json(json_obj) -> ibis_dtypes.JSON: # type: ignore[empty-body]
2103
+ """Convert to JSON."""
2104
2105
2096
2106
@ibis_udf.scalar.builtin(name="to_json_string")
2097
2107
def to_json_string(value) -> ibis_dtypes.String: # type: ignore[empty-body]
2108
"""Convert value to JSON-formatted string."""
bigframes/operations/__init__.py
@@ -124,6 +124,7 @@
124
JSONValue,
125
JSONValueArray,
126
ParseJSON,
127
+ ToJSON,
128
ToJSONString,
129
130
from bigframes.operations.numeric_ops import (
@@ -376,6 +377,7 @@
376
377
"JSONValue",
378
"JSONValueArray",
379
"ParseJSON",
380
+ "ToJSON",
381
"ToJSONString",
382
# Bool ops
383
"and_op",
bigframes/operations/json_ops.py
@@ -102,6 +102,20 @@ def output_type(self, *input_types):
102
return dtypes.JSON_DTYPE
103
104
105
+@dataclasses.dataclass(frozen=True)
106
+class ToJSON(base_ops.UnaryOp):
107
+ name: typing.ClassVar[str] = "to_json"
108
109
+ def output_type(self, *input_types):
110
+ input_type = input_types[0]
111
+ if not dtypes.is_json_encoding_type(input_type):
112
+ raise TypeError(
113
+ "The value to be assigned must be a type that can be encoded as JSON."
114
+ + f"Received type: {input_type}"
115
+ )
116
+ return dtypes.JSON_DTYPE
117
118
119
@dataclasses.dataclass(frozen=True)
120
class ToJSONString(base_ops.UnaryOp):
121
name: typing.ClassVar[str] = "to_json_string"
tests/system/small/bigquery/test_json.py
@@ -386,6 +386,31 @@ def test_parse_json_w_invalid_series_type():
386
bbq.parse_json(s)
387
388
389
+def test_to_json_from_int():
390
+ s = bpd.Series([1, 2, None, 3])
391
+ actual = bbq.to_json(s)
392
+ expected = bpd.Series(["1.0", "2.0", "null", "3.0"], dtype=dtypes.JSON_DTYPE)
393
+ pd.testing.assert_series_equal(actual.to_pandas(), expected.to_pandas())
394
395
396
+def test_to_json_from_struct():
397
+ s = bpd.Series(
398
+ [
399
+ {"version": 1, "project": "pandas"},
400
+ {"version": 2, "project": "numpy"},
401
+ ]
402
403
+ assert dtypes.is_struct_like(s.dtype)
404
405
406
+ expected = bpd.Series(
407
+ ['{"project":"pandas","version":1}', '{"project":"numpy","version":2}'],
408
+ dtype=dtypes.JSON_DTYPE,
409
410
411
412
413
414
def test_to_json_string_from_int():
415
s = bpd.Series([1, 2, None, 3])
416
actual = bbq.to_json_string(s)
0 commit comments