0

I am trying convert the JSON into a pandas dataframe, so that I can parse through the rows and columns, and getting the columns. By getting the columns I can easily choose any parameter from the column. However, for now I am stuck into converting the JSON into a pandas dataframe.

I have tried this : Python - How to convert JSON File to Dataframe, and the below code is something that I am fiddling with. However I am getting this error :

Traceback (most recent call last):
  File "D:\Work\testjson", line 28, in <module>
    df = pd_json.json_normalize (jstr2)
  File "D:\Python 36\lib\site-packages\pandas\io\json\_normalize.py", line 258, in json_normalize
    if any([isinstance(x, dict) for x in y.values()] for y in data):
  File "D:\Python 36\lib\site-packages\pandas\io\json\_normalize.py", line 258, in <genexpr>
    if any([isinstance(x, dict) for x in y.values()] for y in data):
AttributeError: 'str' object has no attribute 'values'
[Finished in 1.9s]
import urllib
from urllib import request
import json
import pandas as pd
import pandas.io.json as pd_json
from pandas.io.json import json_normalize

link = "https://api.worldweatheronline.com/premium/v1/weather.ashx?key=ca926a35ffc14b97b0993747192010&q=London&format=json&num_of_days=5&extra=localObsTime&date=today&fx=yes&cc=yes&mca=yes&fx24=yes&includelocation=yes&show_comments=yes&tp=3&showlocaltime=yes"
#f = request.get (link)

def get_json_data(link):
    with urllib.request.urlopen(link) as f:
        fread = f.read().decode('utf-8')
    return fread 
        #print (f)

fdata = get_json_data(link)
#print (fdata)
# #df = pd.DataFrame.from_dict (json_normalize(fdata), orient = 'columns'
# with open (fdata, 'r', encoding='utf-8') as json_file: 
#   json_work = json.load(json_file)
#   print (json_work)
# #print (df) 
jstr = json.dumps(fdata, sort_keys = True, indent = 4)
jstr2 = json.dumps(fdata, ensure_ascii = False, indent = 4) 
#print (jstr2)

df = pd_json.json_normalize (jstr2)

Am I missing something, or is there something I need to change. Even when I tried to do this

print (fdata.keys())

it gives me the same error.

S_Chakra
  • 27
  • 1
  • 9
  • `json_normalize` expects a `dict or list of dicts` according to the documentation. `json.dumps`returns `Serialize obj to a JSON formatted str...`. So I think that's the root issue here. Someone else might have a solution? – Hampus Larsson Oct 28 '19 at 11:23

0 Answers0