@@ -128,7 +128,8 @@ def test_json_extract_array_from_json():
128128 ['{"a": ["ab", "2", "3 xy"]}' , '{"a": []}' , '{"a": ["4", "5"]}' , "{}" ],
129129 dtype = dtypes .JSON_DTYPE ,
130130 )
131- actual = bbq .json_extract_array (s , "$.a" )
131+ with pytest .warns (UserWarning , match = "The `json_extract_array` is deprecated" ):
132+ actual = bbq .json_extract_array (s , "$.a" )
132133
133134 # This code provides a workaround for issue https://github.com/apache/arrow/issues/45262,
134135 # which currently prevents constructing a series using the pa.list_(db_types.JSONArrrowType())
@@ -241,6 +242,66 @@ def test_json_query_w_invalid_series_type():
241242 bbq .json_query (s , "$.a" )
242243
243244
245+ def test_json_query_array_from_json ():
246+ s = bpd .Series (
247+ ['{"a": ["ab", "2", "3 xy"]}' , '{"a": []}' , '{"a": ["4", "5"]}' , "{}" ],
248+ dtype = dtypes .JSON_DTYPE ,
249+ )
250+ actual = bbq .json_query_array (s , "$.a" )
251+
252+ # This code provides a workaround for issue https://github.com/apache/arrow/issues/45262,
253+ # which currently prevents constructing a series using the pa.list_(db_types.JSONArrrowType())
254+ sql = """
255+ SELECT 0 AS id, [JSON '"ab"', JSON '"2"', JSON '"3 xy"'] AS data,
256+ UNION ALL
257+ SELECT 1, [],
258+ UNION ALL
259+ SELECT 2, [JSON '"4"', JSON '"5"'],
260+ UNION ALL
261+ SELECT 3, null,
262+ """
263+ df = bpd .read_gbq (sql ).set_index ("id" ).sort_index ()
264+ expected = df ["data" ]
265+ expected .index .name = None
266+ expected .name = None
267+
268+ pd .testing .assert_series_equal (actual .to_pandas (), expected .to_pandas ())
269+
270+
271+ def test_json_query_array_from_json_strings ():
272+ s = bpd .Series (
273+ ['{"a": ["ab", "2", "3 xy"]}' , '{"a": []}' , '{"a": ["4","5"]}' , "{}" ],
274+ dtype = pd .StringDtype (storage = "pyarrow" ),
275+ )
276+ actual = bbq .json_query_array (s , "$.a" )
277+ expected = bpd .Series (
278+ [['"ab"' , '"2"' , '"3 xy"' ], [], ['"4"' , '"5"' ], None ],
279+ dtype = pd .ArrowDtype (pa .list_ (pa .string ())),
280+ )
281+
282+ pd .testing .assert_series_equal (actual .to_pandas (), expected .to_pandas ())
283+
284+
285+ def test_json_query_array_from_json_array_strings ():
286+ s = bpd .Series (
287+ ["[1, 2, 3]" , "[]" , "[4,5]" ],
288+ dtype = pd .StringDtype (storage = "pyarrow" ),
289+ )
290+ actual = bbq .json_query_array (s )
291+ expected = bpd .Series (
292+ [["1" , "2" , "3" ], [], ["4" , "5" ]],
293+ dtype = pd .ArrowDtype (pa .list_ (pa .string ())),
294+ )
295+
296+ pd .testing .assert_series_equal (actual .to_pandas (), expected .to_pandas ())
297+
298+
299+ def test_json_query_array_w_invalid_series_type ():
300+ s = bpd .Series ([1 , 2 ])
301+ with pytest .raises (TypeError ):
302+ bbq .json_query_array (s )
303+
304+
244305def test_json_value_from_json ():
245306 s = bpd .Series (
246307 ['{"a": {"b": [1, 2]}}' , '{"a": {"c": 1}}' , '{"a": {"b": 0}}' ],
0 commit comments