Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions python/pyspark/sql/functions/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -20115,11 +20115,49 @@ def get_json_object(col: "ColumnOrName", path: str) -> Column:

Examples
--------
Example 1: Extract a json object from json string

>>> data = [("1", '''{"f1": "value1", "f2": "value2"}'''), ("2", '''{"f1": "value12"}''')]
>>> df = spark.createDataFrame(data, ("key", "jstring"))
>>> df.select(df.key, get_json_object(df.jstring, '$.f1').alias("c0"), \\
... get_json_object(df.jstring, '$.f2').alias("c1") ).collect()
[Row(key='1', c0='value1', c1='value2'), Row(key='2', c0='value12', c1=None)]
>>> df.select(df.key,
... get_json_object(df.jstring, '$.f1').alias("c0"),
... get_json_object(df.jstring, '$.f2').alias("c1")
... ).show()
+---+-------+------+
|key| c0| c1|
+---+-------+------+
| 1| value1|value2|
| 2|value12| NULL|
+---+-------+------+

Example 2: Extract a json object from json array

>>> data = [
... ("1", '''[{"f1": "value1"},{"f1": "value2"}]'''),
... ("2", '''[{"f1": "value12"},{"f2": "value13"}]''')
... ]
>>> df = spark.createDataFrame(data, ("key", "jarray"))
>>> df.select(df.key,
... get_json_object(df.jarray, '$[0].f1').alias("c0"),
... get_json_object(df.jarray, '$[1].f2').alias("c1")
... ).show()
+---+-------+-------+
|key| c0| c1|
+---+-------+-------+
| 1| value1| NULL|
| 2|value12|value13|
+---+-------+-------+

>>> df.select(df.key,
... get_json_object(df.jarray, '$[*].f1').alias("c0"),
... get_json_object(df.jarray, '$[*].f2').alias("c1")
... ).show()
+---+-------------------+---------+
|key| c0| c1|
+---+-------------------+---------+
| 1|["value1","value2"]| NULL|
| 2| "value12"|"value13"|
+---+-------------------+---------+
"""
from pyspark.sql.classic.column import _to_java_column

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ import org.apache.spark.unsafe.types.UTF8String
Examples:
> SELECT _FUNC_('{"a":"b"}', '$.a');
b
> SELECT _FUNC_('[{"a":"b"},{"a":"c"}]', '$[0].a');
b
> SELECT _FUNC_('[{"a":"b"},{"a":"c"}]', '$[*].a');
["b","c"]
""",
group = "json_funcs",
since = "1.5.0")
Expand Down