I am trying to convert a heavily nested JSON file into a data format that can easily be loaded into a database table. I've been using json_normalize
from the pandas
library to flatten various parts of the file into a tabular format. This has mostly worked, however i've come across a section with the following structure:
{"data": {
"Revenue": {
"2019-05-31": 50,
"2018-05-31": 60,
"2017-05-31": 70,
"2016-05-31": 75,
"2015-05-31": 90,
"2014-05-31": 100
},
"Cost of Revenue": {
"2019-05-31": 45,
"2018-05-31": 40,
"2017-05-31": 57,
"2016-05-31": 58,
"2015-05-31": 68,
"2014-05-31": 70
}
}}
I would like to convert this data into the following format (assuming this is the best way to structure this type of data):
measure period end value
Revenue 2019-05-31 50
Revenue 2018-05-31 60
Revenue 2017-05-31 70
Revenue 2016-05-31 75
Revenue 2015-05-31 90
Revenue 2014-05-31 100
Cost of Revenue 2019-05-31 45
Cost of Revenue 2018-05-31 40
Cost of Revenue 2017-05-31 57
Cost of Revenue 2016-05-31 58
Cost of Revenue 2015-05-31 68
Cost of Revenue 2014-05-31 70
I won't know what the 'measure' and 'period end' key names are ahead of time, but it will always have the above structure. I haven't been able to find a generic solution, but i'm fairly new to Python.
Any help or guidance would be much appreciated.