1

Any idea how to normalize the following JSON output from an API:

{
   "AA":{
      "country":"US",
      "currency":"USD",
      "exchange":"NEW YORK STOCK EXCHANGE, INC.",
      "finnhubIndustry":"Metals & Mining",
      "ipo":"2016-10-18",
      "logo":"",
      "marketCapitalization":4348.893,
      "name":"Alcoa Corp",
      "phone":"14123152900",
      "shareOutstanding":185.929586,
      "ticker":"AA",
      "weburl":"https://www.alcoa.com/global/en/home.asp"
   },
   "AAA":{
      
   },

With pd.DataFrame.from_dict(json_normalize(df1), orient='columns') I get:

    AA.country  AA.currency AA.exchange AA.finnhubIndustry  AA.ipo  AA.logo AA.marketCapitalization AA.name AA.phone    AA.shareOutstanding ... AAPL.exchange   AAPL.finnhubIndustry    AAPL.ipo    AAPL.logo   AAPL.marketCapitalization   AAPL.name   AAPL.phone  AAPL.shareOutstanding   AAPL.ticker AAPL.weburl
0   US  USD NEW YORK STOCK EXCHANGE, INC.   Metals & Mining 2016-10-18      4348.893    Alcoa Corp  14123152900 185.929586  ... NASDAQ NMS - GLOBAL MARKET  Technology  1980-12-12  https://static.finnhub.io/logo/87cb30d8-80df-1...   2103973 Apple Inc   14089961010 17001.802   AAPL    https://www.apple.com/
1 rows × 144 columns

But I need a format there AA.XXX is a row and new row is added for AAA.XXX

2 Answers 2

2

You don't need to call pd.json_normalize; just use pd.DataFrame.from_dict and specify the orient argument as index.

Note, I've just duplicated your json into a new node, so the values will be the same.

j = {
   "AA":{
      "country":"US",
      "currency":"USD",
      "exchange":"NEW YORK STOCK EXCHANGE, INC.",
      "finnhubIndustry":"Metals & Mining",
      "ipo":"2016-10-18",
      "logo":"",
      "marketCapitalization":4348.893,
      "name":"Alcoa Corp",
      "phone":"14123152900",
      "shareOutstanding":185.929586,
      "ticker":"AA",
      "weburl":"https://www.alcoa.com/global/en/home.asp"
   },
   "AAA":{
      "country":"US",
      "currency":"USD",
      "exchange":"NEW YORK STOCK EXCHANGE, INC.",
      "finnhubIndustry":"Metals & Mining",
      "ipo":"2016-10-18",
      "logo":"",
      "marketCapitalization":4348.893,
      "name":"Alcoa Corp",
      "phone":"14123152900",
      "shareOutstanding":185.929586,
      "ticker":"AA",
      "weburl":"https://www.alcoa.com/global/en/home.asp"} }

df = pd.DataFrame.from_dict(j,orient='index')

print(df)

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

If you want to use json_normalize, you could get straight to a dataframe by calling it like this:

from pandas import json_normalize

json_normalize([d for d in j.values()])

enter image description here

1 Comment

I changed a few field values of the second node in the json just to make sure it was doing what it was supposed to :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.