Repository: pandas-dev/pandas
File: pandas/tests/io/json/test_normalize.py (Python)

Code before:
                "Image": {"a": "b"},
                "tags": [
                    {"foo": "something", "bar": "else"},
                    {"foo": "something2", "bar": "else2"},
                ],
            }
        ]

        result = json_normalize(
            test_input,
            record_path=["Lookup"],
            meta=[["CreatedBy"], ["Image"]],
            max_level=max_level,
        )
        expected_df = DataFrame(data=expected, columns=result.columns.values)
        tm.assert_equal(expected_df, result)

    def test_json_normalize_non_dict_items(self):
        # gh-62829
        d = {
            1: {"id": 10, "status": "AVAL"},
            2: {"id": 30, "status": "AVAL", "items": {"id": 12, "size": 20}},
            3: {"id": 50, "status": "AVAL", "items": {"id": 13, "size": 30}},
        }
        df = DataFrame.from_dict(d, orient="index")
        data_list = df["items"].tolist()  # [nan, {dict}, {dict}]
        msg = "All items in data must be of type dict, found float"

        with pytest.raises(TypeError, match=msg):
            json_normalize(data_list, max_level=0)

        with pytest.raises(TypeError, match=msg):
            json_normalize(data_list)

    def test_nested_flattening_consistent(self):
        # see gh-21537
        df1 = json_normalize([{"A": {"B": 1}}])
        df2 = json_normalize({"dummy": [{"A": {"B": 1}}]}, "dummy")

        # They should be the same.
        tm.assert_frame_equal(df1, df2)

    def test_nonetype_record_path(self, nulls_fixture):
        # see gh-30148
        # should not raise TypeError
        result = json_normalize(
            [
                {"state": "Texas", "info": nulls_fixture},
                {"state": "Florida", "info": [{"i": 2}]},
            ],
            record_path=["info"],

Review comment (suggestion):
Can you just create `data_list` directly, `data_list = [np.nan, {"id": 12}, {"id": 13}]`. No need for another entry I think.

Code after:
                "Image": {"a": "b"},
                "tags": [
                    {"foo": "something", "bar": "else"},
                    {"foo": "something2", "bar": "else2"},
                ],
            }
        ]

        result = json_normalize(
            test_input,
            record_path=["Lookup"],
            meta=[["CreatedBy"], ["Image"]],
            max_level=max_level,
        )
        expected_df = DataFrame(data=expected, columns=result.columns.values)
        tm.assert_equal(expected_df, result)

    def test_json_normalize_non_dict_items(self):
        # gh-62829
        data_list = [np.nan, {"id": 12}, {"id": 13}]
        msg = "All items in data must be of type dict, found float"

        with pytest.raises(TypeError, match=msg):
            json_normalize(data_list, max_level=0)

        with pytest.raises(TypeError, match=msg):
            json_normalize(data_list)

    def test_nested_flattening_consistent(self):
        # see gh-21537
        df1 = json_normalize([{"A": {"B": 1}}])
        df2 = json_normalize({"dummy": [{"A": {"B": 1}}]}, "dummy")

        # They should be the same.
        tm.assert_frame_equal(df1, df2)

    def test_nonetype_record_path(self, nulls_fixture):
        # see gh-30148
        # should not raise TypeError
        result = json_normalize(
            [
                {"state": "Texas", "info": nulls_fixture},
                {"state": "Florida", "info": [{"i": 2}]},
            ],
            record_path=["info"],
        )
        expected = DataFrame({"i": 2}, index=range(1))
        tm.assert_equal(result, expected)

    @pytest.mark.parametrize("value", ["false", "true", "{}", "1", '"text"'])
    def test_non_list_record_path_errors(self, value):