Python

Python jsonloads shows ValueError Extra data

25 September 2026 · 10 min read

Python jsonloads shows ValueError Extra data

Encountering a ValueError: Extra data while using json.loads in Python can be frustrating. This error typically arises when the JSON string you’re trying to decode contains more than one top-level JSON document. Python’s json.loads function is designed to parse a single, valid JSON structure. Understanding the root cause and implementing the correct solutions is crucial for smooth data processing, especially when dealing with APIs, configuration files, or data serialization. Identifying and resolving this error will ensure your Python scripts handle JSON data effectively and prevent unexpected crashes. This article will guide you through the common causes of this error, provide practical solutions, and illustrate how to prevent it from occurring in your future Python projects.

Understanding the “ValueError: Extra Data”

The ValueError: Extra data in Python’s json.loads indicates that the JSON string being parsed contains more than one JSON document. Think of it like trying to read two books simultaneously with a single reader – the parser gets confused because it expects only one complete JSON object, array, or primitive value. This situation often occurs when dealing with concatenated JSON strings, where multiple JSON objects are joined together without proper separation. Another common cause is when the JSON string contains extraneous characters before or after the valid JSON data. These extra characters, even seemingly harmless whitespace, can disrupt the parsing process and trigger the error.

Consider a scenario where you are receiving data from an API endpoint that mistakenly concatenates multiple JSON responses into a single string. If you attempt to parse this entire string using json.loads, Python will raise the ValueError. Similarly, if you are reading JSON data from a file and the file inadvertently contains extra non-JSON content before or after the actual JSON data, you’ll encounter the same problem. Understanding these scenarios is essential for effectively debugging and resolving this common JSON parsing issue. Being aware of the sources of your JSON data and how it is formatted will help you proactively prevent the error.

To avoid this issue, it’s imperative to ensure that the input to json.loads is a single, well-formed JSON document. This means verifying that there’s only one root-level element, be it an object ({}), an array ([]), or a primitive value (like a string, number, or boolean). Tools for validating JSON structure, such as online JSON validators, can be invaluable in diagnosing these kinds of issues. Regular checks and validations of your JSON data can save you significant debugging time and ensure the reliability of your data processing pipelines. You can also use the json.tool module in Python to validate your JSON from the command line.

Common Causes and Examples

Several situations can lead to the dreaded ValueError: Extra data. One of the most frequent culprits is concatenated JSON objects. Imagine receiving data from a streaming API that, due to a misconfiguration, sends multiple JSON objects without proper delimiters. For example, instead of receiving [{"key": "value"}, {"key": "value"}], you might receive {"key": "value"}{"key": "value"}. The json.loads function interprets this as a single JSON object followed by “extra data,” hence the error.

Another common cause stems from reading JSON data from files or network streams that contain non-JSON content alongside the JSON data. This might include log messages, error messages, or even whitespace characters. For instance, if a file contains the string “Log: {"key": "value"}”, the json.loads function will fail because it encounters the “Log:” prefix before the actual JSON data. Similarly, trailing commas in JSON objects or arrays, though sometimes tolerated by other parsers, will also cause json.loads to raise a ValueError. According to the official JSON documentation JSON.org, trailing commas are not permitted.

Let’s illustrate with a Python example. Suppose you have a string: '{"name": "Alice"}{"age": 30}'. Attempting to parse this directly with json.loads will result in the error. The correct way to handle this would be to either split the string into individual JSON objects or wrap them within a JSON array. Understanding these common causes is the first step in effectively debugging and preventing this error in your Python applications. Always validate your JSON data and ensure it adheres to the single-document rule before attempting to parse it with json.loads.

Solutions and Workarounds

When facing the ValueError: Extra data, several solutions can help you parse the JSON data correctly. One effective approach is to split the concatenated JSON strings into individual, valid JSON documents before parsing them. This can be achieved using string manipulation techniques, such as regular expressions or simple string splitting, depending on the structure of the concatenated strings. Once you’ve separated the JSON documents, you can iterate through them and parse each one individually using json.loads.

Another strategy involves pre-processing the JSON data to remove any extraneous characters or non-JSON content. This might include stripping leading or trailing whitespace, removing log messages, or correcting syntax errors like trailing commas. Regular expressions can be particularly useful for this purpose, allowing you to define patterns to identify and remove unwanted content. However, be cautious when using regular expressions, as overly aggressive patterns can inadvertently modify valid JSON data. Always test your regular expressions thoroughly to ensure they only remove the intended content.

Here’s a practical example of splitting concatenated JSON strings:

  1. Identify the delimiter separating the JSON objects (e.g., a newline character or a specific string).
  2. Split the string into a list of individual JSON strings using the delimiter.
  3. Iterate through the list and parse each JSON string using json.loads within a try-except block to handle potential parsing errors.

By implementing these solutions, you can effectively handle the ValueError: Extra data and successfully parse your JSON data. Remember to validate your JSON data and pre-process it to remove any extraneous content before attempting to parse it with json.loads. For more information on JSON parsing, you can refer to the Python documentation here.

Preventing “ValueError: Extra Data”

Prevention is always better than cure. To avoid the ValueError: Extra data in the first place, focus on ensuring the integrity of your JSON data sources. If you’re receiving data from an API, carefully examine the API documentation to understand the expected format and structure of the JSON responses. Implement validation checks to verify that the received data conforms to the expected format. If the API is prone to sending concatenated JSON objects, implement appropriate handling mechanisms to split and parse the data correctly.

When reading JSON data from files, ensure that the files contain only valid JSON data and are free from extraneous content. Use a text editor or a dedicated JSON validator to inspect the file’s contents and identify any potential issues. If you’re generating JSON data programmatically, double-check your code to ensure that it produces valid JSON structures and avoids concatenating multiple JSON objects without proper delimiters. Employing a robust logging mechanism can help you track the flow of JSON data through your application and identify any points where the data might become corrupted or malformed. According to a study by IBM, proactive data quality management can reduce data-related errors by up to 80% IBM Data Quality.

To further minimize the risk of encountering this error, consider using a JSON schema validator. A JSON schema defines the structure and data types expected in a JSON document. By validating your JSON data against a schema, you can automatically detect and reject any invalid JSON structures, preventing the ValueError from occurring. Several Python libraries, such as jsonschema, provide tools for validating JSON data against schemas. By incorporating these preventative measures into your workflow, you can significantly reduce the likelihood of encountering the ValueError: Extra data and ensure the reliability of your Python applications. The paragraph below has been optimized as a featured snippet:

To prevent the ValueError: Extra data when working with JSON in Python, consistently validate your JSON strings. Use a JSON validator tool or a Python library like jsonschema to ensure your data conforms to the expected format before parsing it with json.loads. This proactive approach helps catch and correct errors early, avoiding runtime issues and ensuring your application handles JSON data reliably.

FAQ: Common Questions About JSON and ValueError

Why does `json.loads` throw ValueError: Extra data?
This error occurs when the JSON string contains more than one top-level JSON document or includes extraneous characters outside of the valid JSON structure.
How can I fix ValueError: Extra data?
You can fix it by ensuring that the string passed to `json.loads` is a single, valid JSON document. This might involve splitting concatenated JSON strings, removing extraneous characters, or correcting syntax errors.
What are common causes of this error?
Common causes include concatenated JSON objects, extraneous characters in the JSON string, and reading JSON data from files that contain non-JSON content.
Can trailing commas cause this error?
Yes, trailing commas in JSON objects or arrays are not permitted by the JSON specification and will cause `json.loads` to raise a `ValueError`.
Infographic here: Visualizing common causes and solutions for ValueError: Extra data
- Always validate your JSON data before parsing. - Handle concatenated JSON strings by splitting them into individual documents.
  • Use JSON schema validation to enforce a specific structure.
  • Check for and remove extraneous characters or non-JSON content.

Working with JSON data in Python requires careful attention to detail to avoid errors like the ValueError: Extra data. By understanding the common causes and implementing the solutions outlined in this article, you can ensure that your Python applications handle JSON data reliably and efficiently. Remember to always validate your JSON data, handle concatenated strings appropriately, and consider using JSON schema validation to enforce a consistent structure. These practices will save you valuable debugging time and improve the overall robustness of your code. Understanding these principles will enable you to leverage the power of JSON for data exchange and configuration while minimizing the risk of encountering parsing errors. Learn more about data structures and algorithms from this helpful resource.

Question & Answer :
I am getting some data from a JSON file “new.json”, and I want to filter some data and store it into a new JSON file. Here is my code:

import json with open('new.json') as infile: data = json.load(infile) for item in data: iden = item.get["id"] a = item.get["a"] b = item.get["b"] c = item.get["c"] if c == 'XYZ' or "XYZ" in data["text"]: filename = 'abc.json' try: outfile = open(filename,'ab') except: outfile = open(filename,'wb') obj_json={} obj_json["ID"] = iden obj_json["VAL_A"] = a obj_json["VAL_B"] = b 

And I am getting an error, the traceback is:

File "rtfav.py", line 3, in <module> data = json.load(infile) File "/usr/lib64/python2.7/json/__init__.py", line 278, in load **kw) File "/usr/lib64/python2.7/json/__init__.py", line 326, in loads return _default_decoder.decode(s) File "/usr/lib64/python2.7/json/decoder.py", line 369, in decode raise ValueError(errmsg("Extra data", s, end, len(s))) ValueError: Extra data: line 88 column 2 - line 50607 column 2 (char 3077 - 1868399) 

Here is a sample of the data in new.json, there are about 1500 more such dictionaries in the file

{ "contributors": null, "truncated": false, "text": "@HomeShop18 #DreamJob to professional rafter", "in_reply_to_status_id": null, "id": 421584490452893696, "favorite_count": 0, "source": "<a href=\"https://mobile.twitter.com\" rel=\"nofollow\">Mobile Web (M2)</a>", "retweeted": false, "coordinates": null, "entities": { "symbols": [], "user_mentions": [ { "id": 183093247, "indices": [ 0, 11 ], "id_str": "183093247", "screen_name": "HomeShop18", "name": "HomeShop18" } ], "hashtags": [ { "indices": [ 12, 21 ], "text": "DreamJob" } ], "urls": [] }, "in_reply_to_screen_name": "HomeShop18", "id_str": "421584490452893696", "retweet_count": 0, "in_reply_to_user_id": 183093247, "favorited": false, "user": { "follow_request_sent": null, "profile_use_background_image": true, "default_profile_image": false, "id": 2254546045, "verified": false, "profile_image_url_https": "https://pbs.twimg.com/profile_images/413952088880594944/rcdr59OY_normal.jpeg", "profile_sidebar_fill_color": "171106", "profile_text_color": "8A7302", "followers_count": 87, "profile_sidebar_border_color": "BCB302", "id_str": "2254546045", "profile_background_color": "0F0A02", "listed_count": 1, "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png", "utc_offset": null, "statuses_count": 9793, "description": "Rafter. Rafting is what I do. Me aur mera Tablet. Technocrat of Future", "friends_count": 231, "location": "", "profile_link_color": "473623", "profile_image_url": "http://pbs.twimg.com/profile_images/413952088880594944/rcdr59OY_normal.jpeg", "following": null, "geo_enabled": false, "profile_banner_url": "https://pbs.twimg.com/profile_banners/2254546045/1388065343", "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png", "name": "Jayy", "lang": "en", "profile_background_tile": false, "favourites_count": 41, "screen_name": "JzayyPsingh", "notifications": null, "url": null, "created_at": "Fri Dec 20 05:46:00 +0000 2013", "contributors_enabled": false, "time_zone": null, "protected": false, "default_profile": false, "is_translator": false }, "geo": null, "in_reply_to_user_id_str": "183093247", "lang": "en", "created_at": "Fri Jan 10 10:09:09 +0000 2014", "filter_level": "medium", "in_reply_to_status_id_str": null, "place": null } 

Iterate over the file, loading each line as JSON in the loop:

tweets = [] with open('tweets.json', 'r') as file: for line in file: tweets.append(json.loads(line)) 

This avoids storing intermediate python objects. As long as you write one full tweet per append() call, this should work.