2

My json file

{
  "Domain": "Hello",
  "Points": 0,
  "Search Name": "wonderful1 hello",
  "UserId": "wonder1",
  "UserName": "Wonderful1 Hello",
  "UserRole": "CONSUMER","CONS"
}

My code:

with open('e:\\data\\datafile.json') as f: 
    content = f.read()
df=pd.read_json('[' + content.replace('}\n', '},') + ']')

unable to load the into dataframe. If i get one more than one value in UserRole column. I was not able to load the data? Can you throw some lights on this?

2
  • Your JSON is not valid. How do you expect it to be loaded in a dataframe? Commented Apr 8, 2020 at 13:31
  • remove "CONS" try ; Commented Apr 8, 2020 at 13:36

2 Answers 2

2

I believe you need json.load for convert json to dicts or list of dicts first:

import json
from pandas.io.json import json_normalize    

with open('data.json') as data_file:    
    data = json.load(data_file)  

And then if necessary json_normalize:

df = json_normalize(data)
print (df)
  Domain  Points       Search Name   UserId          UserName  UserRole
0  Hello       0  wonderful1 hello  wonder1  Wonderful1 Hello  CONSUMER

Or if there is only one dictionary in data like in sample:

df = pd.DataFrame([data])
print (df)
  Domain  Points       Search Name   UserId          UserName  UserRole
0  Hello       0  wonderful1 hello  wonder1  Wonderful1 Hello  CONSUMER

Or if there are list of dictionaries:

df = pd.DataFrame(data)

EDIT:

Because not valid json file one possible solution is use yaml.load:

import yaml
from pandas.io.json import json_normalize    

with open('data.json') as data_file:    
    data = yaml.load(data_file, Loader=yaml.FullLoader)  

print (data)
{'Domain': 'Hello', 'Points': 0, 'Search Name': 'wonderful1 hello',
 'UserId': 'wonder1', 'UserName': 'Wonderful1 Hello', 
 'UserRole': 'CONSUMER', 'CONS': None}

df = json_normalize(data)
print (df)
  Domain  Points       Search Name   UserId          UserName  UserRole  CONS
0  Hello       0  wonderful1 hello  wonder1  Wonderful1 Hello  CONSUMER  None
Sign up to request clarification or add additional context in comments.

4 Comments

If i add two or more values in UserRole. I was not able to load. for example: "UserRole": "CONSUMER","CONS". How can i load the data into dataframe
@Gower - So json is not valid?
above mentioned code is working. but if i add one more value in Json file @ UserRole Column. I was not able to load in dataframe. "UserRole": "CONSUMER","CONS"
@Gower - If invalid json file, not easy. Can you check if working solution in last paragraph?
0

Lets try a very simple way

import json
import pandas as pd

with open('data.json', 'r') as data_file:  
    content = data_file.read()

data = json.loads(content)
print(type(data)) # it should be a dict
df = pd.DataFrame.from_records([data])

I've checked the code. It works well.

4 Comments

If remove from_records it is same like my answer (from_records is redundant here)
You're right. The difference is minor. Your solution should work.
Not working . getting error message Expecting ':' delimiter: line 8 column 1 (char 164)
Have you got a dictionary from your json?

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.