8
8
import numpy as np
9
9
10
10
from pandas ._libs .writers import convert_json_to_lines
11
+ from pandas ._typing import Scalar
11
12
from pandas .util ._decorators import deprecate
12
13
13
14
import pandas as pd
@@ -230,14 +231,28 @@ def _json_normalize(
230
231
Returns normalized data with columns prefixed with the given string.
231
232
"""
232
233
233
- def _pull_field (js : Dict [str , Any ], spec : Union [List , str ]) -> Iterable :
234
+ def _pull_field (
235
+ js : Dict [str , Any ], spec : Union [List , str ]
236
+ ) -> Union [Scalar , Iterable ]:
237
+ """Internal function to pull field"""
234
238
result = js # type: ignore
235
239
if isinstance (spec , list ):
236
240
for field in spec :
237
241
result = result [field ]
238
242
else :
239
243
result = result [spec ]
244
+ return result
245
+
246
+ def _pull_records (js : Dict [str , Any ], spec : Union [List , str ]) -> Iterable :
247
+ """
248
+ Interal function to pull field for records, and similar to
249
+ _pull_field, but require to return Iterable. And will raise error
250
+ if has non iterable value.
251
+ """
252
+ result = _pull_field (js , spec )
240
253
254
+ # GH 31507 GH 30145, if result is not Iterable, raise TypeError if not
255
+ # null, otherwise return an empty list
241
256
if not isinstance (result , Iterable ):
242
257
if pd .isnull (result ):
243
258
result = [] # type: ignore
@@ -246,7 +261,6 @@ def _pull_field(js: Dict[str, Any], spec: Union[List, str]) -> Iterable:
246
261
f"{ js } has non iterable value { result } for path { spec } . "
247
262
"Must be iterable or null."
248
263
)
249
-
250
264
return result
251
265
252
266
if isinstance (data , list ) and not data :
@@ -296,7 +310,7 @@ def _recursive_extract(data, path, seen_meta, level=0):
296
310
_recursive_extract (obj [path [0 ]], path [1 :], seen_meta , level = level + 1 )
297
311
else :
298
312
for obj in data :
299
- recs = _pull_field (obj , path [0 ])
313
+ recs = _pull_records (obj , path [0 ])
300
314
recs = [
301
315
nested_to_record (r , sep = sep , max_level = max_level )
302
316
if isinstance (r , dict )
0 commit comments