In Python ValueError: Trailing data occurs when you try to load the JSON data or file into pandas DataFrame, and the data is written in lines separated with newline characters such as ‘\n’.
Typically, we import data from the JSON files, and there are higher chances that JSON data contains newline characters.
Let’s take a simple example to reproduce this error. We have a JSON file of employees, and the address property in the JSON has ‘\n’
# import pandas library
import pandas as pd
# create pandas DataFrame
df = pd.read_json('employee.json')
# print names of employee
print(df)
Output
ValueError: Trailing data
Note: If the JSON data is malformed or the file path is invalid you will get an error ValueError: Expected object or value
Solution Python ValueError: Trailing data
The simplest way to fix this error is to pass the lines=True
argument in the read_json() method while importing the JSON file.
The lines=True
parameter ensures to read the JSON file as an object per line.
Now when we import the JSON file into a pandas DataFrame, it will load and print the data without any issue.
# import pandas library
import pandas as pd
# create pandas DataFrame
df = pd.read_json('employee.json',lines=True)
# print names of employee
print(df)
Output
ID name age address
0 123 Jack 25 #3, 5th Main \nIndia
1 124 Chandler 25 #5, 2nd Main \nUS
2 123 Jack 25 #3/2, 6th Main \nCanada
Another way is to remove the \n
character from the address column. We can simply replace the \n character with an empty ''
character, as shown below.
# import pandas library
import pandas as pd
# create pandas DataFrame
df = pd.read_json('employee.json',lines=True)
df['address'] = df['address'].str.replace('\n', ' ')
# print names of employee
print(df)
Output
ID name age address
0 123 Jack 25 #3, 5th Main India
1 124 Chandler 25 #5, 2nd Main US
2 123 Jack 25 #3/2, 6th Main Canada