Skip to content

Commit c120ab5

Browse files
committed
whatsnew and adjusted for precommit
1 parent 3b2838a commit c120ab5

File tree

3 files changed

+20
-42
lines changed

3 files changed

+20
-42
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Other enhancements
3535
- Added missing :meth:`pandas.Series.info` to API reference (:issue:`60926`)
3636
- :class:`pandas.api.typing.NoDefault` is available for typing ``no_default``
3737
- :func:`DataFrame.to_excel` now raises an ``UserWarning`` when the character count in a cell exceeds Excel's limitation of 32767 characters (:issue:`56954`)
38+
- :func:`json_normalize` now supports parsing JSON strings and bytes directly, eliminating the need for an intermediate apply(json.loads) step (:issue:`61006`)
3839
- :func:`pandas.merge` now validates the ``how`` parameter input (merge type) (:issue:`59435`)
3940
- :func:`pandas.merge`, :meth:`DataFrame.merge` and :meth:`DataFrame.join` now support anti joins (``left_anti`` and ``right_anti``) in the ``how`` parameter (:issue:`42916`)
4041
- :func:`read_spss` now supports kwargs to be passed to pyreadstat (:issue:`56356`)

pandas/io/json/_normalize.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,15 +443,17 @@ def json_normalize(
443443
"""
444444
if isinstance(data, (str, bytes)):
445445
import json
446+
446447
data = json.loads(data)
447-
448+
448449
if isinstance(data, Series):
449450
if data.empty:
450451
return DataFrame()
451-
452+
452453
sample = data.iloc[0]
453454
if isinstance(sample, (str, bytes)):
454455
import json
456+
455457
data = data.apply(json.loads)
456458
index = data.index
457459
else:

pandas/tests/io/json/test_normalize.py

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,9 @@ def test_empty_array(self):
164164
@pytest.mark.parametrize(
165165
"data, record_path, exception_type, expected",
166166
[
167-
# List of dicts
168167
([{"a": 0}, {"a": 1}], None, None, DataFrame([0, 1], columns=["a"])),
169-
170-
# Dict with array
171168
({"a": [{"a": 0}, {"a": 1}]}, "a", None, DataFrame([0, 1], columns=["a"])),
172-
173-
# JSON string of array
174169
('[{"a": 0}, {"a": 1}]', None, None, DataFrame([0, 1], columns=["a"])),
175-
176-
# None input
177170
(None, None, NotImplementedError, None),
178171
],
179172
)
@@ -579,60 +572,42 @@ def test_json_string_input(self):
579572
# GH61006: Accept JSON as str input
580573
json_str = '{"id": 1, "name": {"first": "John", "last": "Doe"}}'
581574
result = json_normalize(json_str)
582-
expected = DataFrame({
583-
"id": [1],
584-
"name.first": ["John"],
585-
"name.last": ["Doe"]
586-
})
575+
expected = DataFrame({"id": [1], "name.first": ["John"], "name.last": ["Doe"]})
587576
tm.assert_frame_equal(result, expected)
588577

589-
json_array_str = '''[
578+
json_array_str = """[
590579
{"id": 1, "name": {"first": "John", "last": "Doe"}},
591580
{"id": 2, "name": {"first": "Jane", "last": "Smith"}}
592-
]'''
581+
]"""
593582
result = json_normalize(json_array_str)
594-
expected = DataFrame({
595-
"id": [1, 2],
596-
"name.first": ["John", "Jane"],
597-
"name.last": ["Doe", "Smith"]
598-
})
583+
expected = DataFrame(
584+
{
585+
"id": [1, 2],
586+
"name.first": ["John", "Jane"],
587+
"name.last": ["Doe", "Smith"],
588+
}
589+
)
599590
tm.assert_frame_equal(result, expected)
600591

601592
def test_json_bytes_input(self):
602593
# GH61006: Accept JSON as bytes input
603594
json_bytes = b'{"id": 1, "name": {"first": "John", "last": "Doe"}}'
604595
result = json_normalize(json_bytes)
605-
expected = DataFrame({
606-
"id": [1],
607-
"name.first": ["John"],
608-
"name.last": ["Doe"]
609-
})
596+
expected = DataFrame({"id": [1], "name.first": ["John"], "name.last": ["Doe"]})
610597
tm.assert_frame_equal(result, expected)
611598

612599
def test_series_json_string(self):
613600
# GH61006:
614-
s = Series([
615-
'{"value": 0.0}',
616-
'{"value": 0.5}',
617-
'{"value": 1.0}'
618-
])
601+
s = Series(['{"value": 0.0}', '{"value": 0.5}', '{"value": 1.0}'])
619602
result = json_normalize(s)
620-
expected = DataFrame({
621-
"value": [0.0, 0.5, 1.0]
622-
})
603+
expected = DataFrame({"value": [0.0, 0.5, 1.0]})
623604
tm.assert_frame_equal(result, expected)
624605

625606
def test_series_json_string_with_index(self):
626607
# GH61006:
627-
s = Series(
628-
['{"value": 0.0}', '{"value": 0.5}'],
629-
index=['a', 'b']
630-
)
608+
s = Series(['{"value": 0.0}', '{"value": 0.5}'], index=["a", "b"])
631609
result = json_normalize(s)
632-
expected = DataFrame(
633-
{"value": [0.0, 0.5]},
634-
index=['a', 'b']
635-
)
610+
expected = DataFrame({"value": [0.0, 0.5]}, index=["a", "b"])
636611
tm.assert_frame_equal(result, expected)
637612

638613
def test_invalid_json_string(self):

0 commit comments

Comments
 (0)