88import numpy as np
99
1010from pandas ._libs .writers import convert_json_to_lines
11+ from pandas ._typing import Scalar
1112from pandas .util ._decorators import deprecate
1213
1314import pandas as pd
@@ -230,14 +231,28 @@ def _json_normalize(
230231 Returns normalized data with columns prefixed with the given string.
231232 """
232233
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"""
234238 result = js # type: ignore
235239 if isinstance (spec , list ):
236240 for field in spec :
237241 result = result [field ]
238242 else :
239243 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 )
240253
254+ # GH 31507 GH 30145, if result is not Iterable, raise TypeError if not
255+ # null, otherwise return an empty list
241256 if not isinstance (result , Iterable ):
242257 if pd .isnull (result ):
243258 result = [] # type: ignore
@@ -246,7 +261,6 @@ def _pull_field(js: Dict[str, Any], spec: Union[List, str]) -> Iterable:
246261 f"{ js } has non iterable value { result } for path { spec } . "
247262 "Must be iterable or null."
248263 )
249-
250264 return result
251265
252266 if isinstance (data , list ) and not data :
@@ -296,7 +310,7 @@ def _recursive_extract(data, path, seen_meta, level=0):
296310 _recursive_extract (obj [path [0 ]], path [1 :], seen_meta , level = level + 1 )
297311 else :
298312 for obj in data :
299- recs = _pull_field (obj , path [0 ])
313+ recs = _pull_records (obj , path [0 ])
300314 recs = [
301315 nested_to_record (r , sep = sep , max_level = max_level )
302316 if isinstance (r , dict )
0 commit comments