How to Fix in Python ValueError: Trailing data?

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’ 

Valueerror: Expected Object Or Value
JSON File
# 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
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Python Bool()

Python bool()

Table of Contents Hide bool() Syntax bool() Parametersbool() Return ValueExample of bool() function in Python Python’s bool() function converts a given value into Boolean(True or False) using the standard truth testing…
View Post
Convert String To Float In Python

Convert String to Float in Python

Table of Contents Hide Convert string to float in Pythonfloat() functionExample 1 – Convert string to floatExample 2 – Default float conversion without any input parameterExample 3 – Convert a…
View Post
Python Callable()

Python callable()

Table of Contents Hide callable() Syntax callable() Parameterscallable() Return ValueExample 1: How does callable() works?Example 2: When an Object is callable Example 3: When an Object is NOT callable  The callable() function in…
View Post
Python Ternary Operator

Python Ternary Operator

Python Ternary operators also called conditional expressions, are operators that evaluate something based on a binary condition. Ternary operators provide a shorthand way to write conditional statements, which makes the…
View Post