@@ -569,6 +569,76 @@ def test_series_index(self, state_data):
569569 result = json_normalize (series , "counties" )
570570 tm .assert_index_equal (result .index , idx .repeat ([3 , 2 ]))
571571
572+ def test_json_string_input (self ):
573+ # GH61006: Accept JSON as str input
574+ json_str = '{"id": 1, "name": {"first": "John", "last": "Doe"}}'
575+ result = json_normalize (json_str )
576+ expected = DataFrame ({
577+ "id" : [1 ],
578+ "name.first" : ["John" ],
579+ "name.last" : ["Doe" ]
580+ })
581+ tm .assert_frame_equal (result , expected )
582+
583+ json_array_str = '''[
584+ {"id": 1, "name": {"first": "John", "last": "Doe"}},
585+ {"id": 2, "name": {"first": "Jane", "last": "Smith"}}
586+ ]'''
587+ result = json_normalize (json_array_str )
588+ expected = DataFrame ({
589+ "id" : [1 , 2 ],
590+ "name.first" : ["John" , "Jane" ],
591+ "name.last" : ["Doe" , "Smith" ]
592+ })
593+ tm .assert_frame_equal (result , expected )
594+
595+ def test_json_bytes_input (self ):
596+ # GH61006: Accept JSON as bytes input
597+ json_bytes = b'{"id": 1, "name": {"first": "John", "last": "Doe"}}'
598+ result = json_normalize (json_bytes )
599+ expected = DataFrame ({
600+ "id" : [1 ],
601+ "name.first" : ["John" ],
602+ "name.last" : ["Doe" ]
603+ })
604+ tm .assert_frame_equal (result , expected )
605+
606+ def test_series_json_string (self ):
607+ # GH61006:
608+ s = Series ([
609+ '{"value": 0.0}' ,
610+ '{"value": 0.5}' ,
611+ '{"value": 1.0}'
612+ ])
613+ result = json_normalize (s )
614+ expected = DataFrame ({
615+ "value" : [0.0 , 0.5 , 1.0 ]
616+ })
617+ tm .assert_frame_equal (result , expected )
618+
619+ def test_series_json_string_with_index (self ):
620+ # GH61006:
621+ s = Series (
622+ ['{"value": 0.0}' , '{"value": 0.5}' ],
623+ index = ['a' , 'b' ]
624+ )
625+ result = json_normalize (s )
626+ expected = DataFrame (
627+ {"value" : [0.0 , 0.5 ]},
628+ index = ['a' , 'b' ]
629+ )
630+ tm .assert_frame_equal (result , expected )
631+
632+ def test_invalid_json_string (self ):
633+ invalid_json = '{"id": 1, "name": {"first": "John", "last": "Doe"'
634+ with pytest .raises (json .JSONDecodeError ):
635+ json_normalize (invalid_json )
636+
637+ def test_non_json_string (self ):
638+ non_json = "Hello World"
639+ with pytest .raises (json .JSONDecodeError ):
640+ json_normalize (non_json )
641+
572642
573643class TestNestedToRecord :
574644 def test_flat_stays_flat (self ):
0 commit comments