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
@@ -226,14 +227,28 @@ def _json_normalize(
226
227
Returns normalized data with columns prefixed with the given string.
227
228
"""
228
229
229
- def _pull_field (js : Dict [str , Any ], spec : Union [List , str ]) -> Iterable :
230
+ def _pull_field (
231
+ js : Dict [str , Any ], spec : Union [List , str ]
232
+ ) -> Union [Scalar , Iterable ]:
233
+ """Internal function to pull field"""
230
234
result = js # type: ignore
231
235
if isinstance (spec , list ):
232
236
for field in spec :
233
237
result = result [field ]
234
238
else :
235
239
result = result [spec ]
240
+ return result
241
+
242
+ def _pull_records (js : Dict [str , Any ], spec : Union [List , str ]) -> Iterable :
243
+ """
244
+ Interal function to pull field for records, and similar to
245
+ _pull_field, but require to return Iterable. And will raise error
246
+ if has non iterable value.
247
+ """
248
+ result = _pull_field (js , spec )
236
249
250
+ # GH 31507 GH 30145, if result is not Iterable, raise TypeError if not
251
+ # null, otherwise return an empty list
237
252
if not isinstance (result , Iterable ):
238
253
if pd .isnull (result ):
239
254
result = [] # type: ignore
@@ -242,7 +257,6 @@ def _pull_field(js: Dict[str, Any], spec: Union[List, str]) -> Iterable:
242
257
f"{ js } has non iterable value { result } for path { spec } . "
243
258
"Must be iterable or null."
244
259
)
245
-
246
260
return result
247
261
248
262
if isinstance (data , list ) and not data :
@@ -292,7 +306,7 @@ def _recursive_extract(data, path, seen_meta, level=0):
292
306
_recursive_extract (obj [path [0 ]], path [1 :], seen_meta , level = level + 1 )
293
307
else :
294
308
for obj in data :
295
- recs = _pull_field (obj , path [0 ])
309
+ recs = _pull_records (obj , path [0 ])
296
310
recs = [
297
311
nested_to_record (r , sep = sep , max_level = max_level )
298
312
if isinstance (r , dict )
0 commit comments