I am curious how I can use pandas to read nested json with the following structure:
{ "number": "", "date": "01.10.2016", "name": "R 3932", "locations": [ { "depTimeDiffMin": "0", "name": "Spital am Pyhrn Bahnhof", "arrTime": "", "depTime": "06:32", "platform": "2", "stationIdx": "0", "arrTimeDiffMin": "", "track": "R 3932" }, { "depTimeDiffMin": "0", "name": "Windischgarsten Bahnhof", "arrTime": "06:37", "depTime": "06:40", "platform": "2", "stationIdx": "1", "arrTimeDiffMin": "1", "track": "" }, { "depTimeDiffMin": "", "name": "Linz/Donau Hbf", "arrTime": "08:24", "depTime": "", "platform": "1A-B", "stationIdx": "22", "arrTimeDiffMin": "1", "track": "" } ] }
The array is stored here as json. I would prefer it to be expanded into columns.
pd.read_json("/myJson.json", orient='records')
Edit
Thanks for the first answers. I have to clarify my question: Smoothing nested attributes in an array is optional. It would be normal only [A, B, C] to combine df.locations ['name'].
My file contains several JSON objects (one per line). I would like to keep a column of number, date, name and locations. However, I will need to join the location.
allLocations = "" isFirst = True for location in result.locations: if isFirst: isFirst = False allLocations = location['name'] else: allLocations += "; " + location['name'] allLocations
My approach here does not seem effective / pandas style.