Reputation: 475
I have an example json dataset as :
['[{"id":"123","product":"dell","date":"2019-01-01","sales":5,"created_at":"2019-01-26 15:00:00"}, {"id":"124","product":"apple","date":"2019-01-02","sales":7,"created_at":"2019-01-27 15:00:00"}]']
I would like to create a pandas dataframe from this json data but when i use the json_normalize method i get AttributeError: 'str' object has no attribute 'values'.
The expected output should be like this:
id product date sales created_at
123. dell. 2019-01-01. 5. 2019-01-26 15:00:00
124. apple. 2019-01-02. 7. 2019-01-27 15:00:00
Upvotes: 0
Views: 251
Reputation: 21
import json
import pandas as pd
x = ['[{"id":"123","product":"dell","date":"2019-01-01","sales":5,"created_at":"2019-01-26 15:00:00"}, {"id":"124","product":"apple","date":"2019-01-02","sales":7,"created_at":"2019-01-27 15:00:00"}]']
# Take 0th element of list
x = x[0]
info = json.loads(x)
df = pd.json_normalize(info)
df
Upvotes: 0
Reputation: 23753
thats when i get the output which is a list of string
If the result of json.load... is like
a = ['[{"id":"123","product":"dell","date":"2019-01-01","sales":5,"created_at":"2019-01-26 15:00:00"}, {"id":"124","product":"apple","date":"2019-01-02","sales":7,"created_at":"2019-01-27 15:00:00"}]']
Then do it again...
>>> b = json.loads(a[0])
>>> b
[{'id': '123', 'product': 'dell', 'date': '2019-01-01', 'sales': 5, 'created_at': '2019-01-26 15:00:00'}, {'id': '124', 'product': 'apple', 'date': '2019-01-02', 'sales': 7, 'created_at': '2019-01-27 15:00:00'}]
>>> pd.DataFrame(b)
id product date sales created_at
0 123 dell 2019-01-01 5 2019-01-26 15:00:00
1 124 apple 2019-01-02 7 2019-01-27 15:00:00
>>>
Unfortunately you will not have the luxury of knowing in advance that you need to do this. You would need to inspect the data first. Unless you are lucky enough to have the specifications of the thing that is making these jsons.
Upvotes: 2