Table of contents
1.
Introduction
2.
Fixing Error
3.
FAQs
4.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

'ValueError: Trailing data' error Resolution

Author soham Medewar
0 upvote

Introduction

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"}
You can also try this code with Online Python Compiler
Run Code

Now, we will import this JSON file into pandas.

data = pd.read_json('data.json')
print(data.head())
You can also try this code with Online Python Compiler
Run Code

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)
print(data.head(6))
You can also try this code with Online Python Compiler
Run Code
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

FAQs

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!

Live masterclass