5

I am trying to import as a dataframe a URL that has a JSON file in it.

import urllib.request, json 
import pandas as pd

with urllib.request.urlopen("https://financialmodelingprep.com/api/v3/company-key-metrics/AAPL?period=quarter") as url:
    data = json.loads(url.read().decode())
    df = pd.DataFrame(data)
print(df)

It is not considering each metric in the JSON file as a column, but puts all the metrics under one column called "metrics"

while the output I am expecting is

enter image description here

3 Answers 3

4

Let's try this a couple of other ways

Option 1 using pd.read_json:

pd.concat([pd.DataFrame(i, index=[0]) 
           for i in 
           pd.read_json('https://financialmodelingprep.com/api/v3/company-key-metrics/AAPL?period=quarter')['metrics']], 
          ignore_index=True)

Option 2 using requests:

import requests

resp = requests.get('https://financialmodelingprep.com/api/v3/company-key-metrics/AAPL?period=quarter')
txt = resp.json()
pd.DataFrame(txt['metrics'])
Sign up to request clarification or add additional context in comments.

Comments

2

You can try this:

import urllib.request, json 
import pandas as pd
from pandas.io.json import json_normalize

with urllib.request.urlopen('https://financialmodelingprep.com/api/v3/company-key-metrics/AAPL?period=quarter') as url:
    data = json.loads(url.read().decode())
    df = pd.DataFrame(json_normalize(data, 'metrics'))
print(df)

Comments

0

This worked smooth for me

import requests
import pandas as pd

resp = requests.get('https://api.binance.com/api/v3/ticker/24hr', timeout=10,headers={'Content-Type': 'application/json'})
df= pd.read_json(resp.text)

print(df)

enter image description here

Comments

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.