Skip to content

Commit df03c91

Browse files
committed
feat: add bigframes.bigquery.json_value
1 parent 5c125c9 commit df03c91

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

bigframes/bigquery/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
json_extract_array,
3939
json_extract_string_array,
4040
json_set,
41+
json_value,
4142
parse_json,
4243
)
4344
from bigframes.bigquery._operations.search import create_vector_index, vector_search
@@ -61,6 +62,7 @@
6162
"json_extract",
6263
"json_extract_array",
6364
"json_extract_string_array",
65+
"json_value",
6466
"parse_json",
6567
# search ops
6668
"create_vector_index",

bigframes/bigquery/_operations/json.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,39 @@ def json_extract_string_array(
231231
return array_series
232232

233233

234+
def json_value(
235+
input: series.Series,
236+
json_path: str,
237+
) -> series.Series:
238+
"""Extracts a JSON scalar value and converts it to a SQL ``STRING`` value. In
239+
addtion, this function:
240+
- Removes the outermost quotes and unescapes the values.
241+
- Returns a SQL ``NULL`` if a non-scalar value is selected.
242+
- Uses double quotes to escape invalid ``JSON_PATH`` characters in JSON keys.
243+
244+
**Examples:**
245+
246+
>>> import bigframes.pandas as bpd
247+
>>> import bigframes.bigquery as bbq
248+
>>> bpd.options.display.progress_bar = None
249+
250+
>>> s = bpd.Series(['{"name": "Jakob", "age": "6"}'])
251+
>>> bbq.json_value(s, json_path="$.age")
252+
0 6
253+
dtype: string
254+
255+
Args:
256+
input (bigframes.series.Series):
257+
The Series containing JSON data (as native JSON objects or JSON-formatted strings).
258+
json_path (str):
259+
The JSON path identifying the data that you want to obtain from the input.
260+
261+
Returns:
262+
bigframes.series.Series: A new Series with the JSON-formatted STRING.
263+
"""
264+
return input._apply_unary_op(ops.JSONValue(json_path=json_path))
265+
266+
234267
@utils.preview(name="The JSON-related API `parse_json`")
235268
def parse_json(
236269
input: series.Series,

0 commit comments

Comments
 (0)