Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
Fixing Error
Key Takeaways
Last Updated: Mar 27, 2024

'ValueError: Trailing data' error Resolution

Author soham Medewar
0 upvote
Crack Google SDE interview : Essential projects
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM


Whenever we read a dataset from the .json file, we often get a 'ValueError: Trailing data' error. Whenever we try to import a JSON file into a pandas dataframe, this type of error can take place even if the data is written in separate lines by using '\n'.

For example, below is the content of the JSON file, and we will try to read the data normally using pandas.

{"Id": 1, "Language": "Java", "Inventor": "Jame\nGosling" }
{"Id": 2, "Language": "C", "Inventor": "Dennis\nRitchie" }
{"Id": 3, "Language": "C++", "Inventor": "Bjarne\nStroustrup"}
{"Id": 4, "Language": "Python", "Inventor": "Guido\nvan Rossum"}
{"Id": 5, "Language": "PHP", "Inventor": "Rasmus\nLerdorf"}
{"Id": 6, "Language": "Javascript", "Inventor": "Brendan\nEich"}

Now, we will import this JSON file into pandas.

data = pd.read_json('data.json')

We will get the following ValueError.

This is because the “Inventor” item in our JSON file contains ‘\n’ to indicate endlines, so we get a ValueError.

Also, see -  Locally Weighted Regression.

Fixing Error

To fix this ValueError, we need to add an additional attribute, "lines" while reading the dataset. We need to make this "lines" attribute true so that pandas read the file as a JSON object per line.

The below code will perfectly read the dataset that is present in JSON format.

data = pd.read_json('data.json', lines = True)
Id    Language            Inventor
0   1        Java       Jame\nGosling
1   2           C     Dennis\nRitchie
2   3         C++  Bjarne\nStroustrup
3   4      Python   Guido\nvan Rossum
4   5         PHP     Rasmus\nLerdorf
5   6  Javascript       Brendan\nEich
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job


1. What is ValueError in Python?

When a user provides an invalid value to a function but a valid parameter, a ValueError is raised in Python.


2. What is ValueError: Trailing data in Python?

When you try to import a JSON file into a pandas DataFrame, but the data is written in lines separated by endlines like 'n', you'll get this problem.


3. Why do we need JSON?

JSON is a serialization and transmission standard for structured data over a network connection. It's mostly used to send data from a server to web apps.

Key Takeaways

In this article, we have discussed why ValueError: Trailing data is caused during reading data from JSON files. Also, we have discussed how to fix the error.

Want to learn more about Machine Learning? Here is an excellent course that can guide you in learning. 

Happy Coding!

Previous article
Binning in Data Mining
Next article
Categorical Data  - Intro & Hands-On
Live masterclass