Home > OS >  Convert Multiple Nested JSON to Pandas Dataframe
Convert Multiple Nested JSON to Pandas Dataframe

Time:12-20

Here's the response I get from my stock data API provider:

    {'status': {'code': 0, 'message': ''},
 'data': {'symbol': 'ESHRAQ',
  'company': 'Eshraq properties Co.',
  'exchange': 'ABU_DHABI',
  'prices': {'columns': ['date', 'open', 'high', 'low', 'close', 'volume'],
   'values': [['2021-12-16T10:00:00Z', 0.39, 0.39, 0.39, 0.39, 4140513],
    ['2021-12-19T10:00:00Z', 0.0, 0.0, 0.35, 0.38, 19006953]]}}}

I need the results to be a simple Pandas dataframe with the following columns: date, company, open, high, low, close, volume.

CodePudding user response:

d = your_json['data']
df = pd.DataFrame(d['prices']['values'], columns=d['prices']['columns']).assign(company=d['company'])

Output:

>>> df
                   date  open  high   low close    volume                company
0  2021-12-16T10:00:00Z  0.39  0.39  0.39  0.39   4140513  Eshraq properties Co.
1  2021-12-19T10:00:00Z  0.00  0.00  0.35  0.38  19006953  Eshraq properties Co.
  • Related