Avoids exception when pandas.io.json.json_normalize contains items in… by dickreuter · Pull Request #14505 · pandas-dev/pandas (original) (raw)

This would be the test, but unclear where I should store it. Any suggestions?

from unittest import TestCase
from pandas.io.json import json_normalize


class Tester(TestCase):
    def test_json_normalise_fix(self):
        j = {
            "Trades": [{
                "general": {
                    "tradeid": 100,
                    "trade_version": 1,
                    "stocks": [{

                        "symbol": "AAPL",
                        "name": "Apple",
                        "price": "0"

                    }, {

                        "symbol": "GOOG",
                        "name": "Google",
                        "price": "0"

                    }
                    ]
                },
            }, {
                "general": {
                    "tradeid": 100,
                    "stocks": [{

                        "symbol": "AAPL",
                        "name": "Apple",
                        "price": "0"

                    }, {
                        "symbol": "GOOG",
                        "name": "Google",
                        "price": "0"

                    }
                    ]
                },
            }
            ]
        }
        j = json_normalize(data=j['Trades'], record_path=[['general', 'stocks']],
                           meta=[['general', 'tradeid'], ['general', 'trade_version']])
        self.assertEqual(len(j), 4)