Json parse error extra data line 1 column 11 char 10

If you want to load a JSON file using json.loads() and you have multiple records not contained in an array, you will raise the ValueError: extra data. The

If you want to load a JSON file using json.loads() and you have multiple records not contained in an array, you will raise the ValueError: extra data. The method json.loads() is not able to decode more than one record at once.

You can solve this error by reformatting your JSON file to contain an array or by reading the JSON file line by line, for example:

data = [json.loads(line) for line in open('extra.json','r')]

This tutorial will go through the error in detail and how to solve it with code examples.


Table of contents

  • JSONDecodeError: extra data
  • Example
    • Solution #1: Reformat the JSON file
    • Solution #2: Use List Comprehension with json.loads()
  • Summary

In Python, JSONDecodeError occurs when there is an issue with the formatting of the JSON data. In this specific case, the JSON file contains multiple JSON strings. The json.loads() method can only parse one JSON string at a time.

Value: extra data

Python developers have also encountered the error as a ValueError: extra data. In Python, a value is a piece of information stored within a particular object. We will encounter a ValueError in Python when using a built-in operation or function that receives an argument that is the right type but an inappropriate value. The data we want to read is the correct type, JSON string, but the file contains multiple JSON strings that are not inside an array, which is an inappropriate format.

Example

Let’s look at an example where we want to read JSON data into a program using json.loads(). First, let’s look at the JSON data, which contains information about five different pizzas.

{"pizza":"margherita", "price":7.99, "Details":"Contains cheese. Suitable for vegetarians"}
{"pizza":"pepperoni", "price":9.99, "Details":"Contains meat. Not suitable for vegetarians"}
{"pizza":"marinara", "price":6.99, "Details":"Dairy free. Suitable for vegetarians."}
{"pizza":"four cheese", "price":10.99, "Details":"Contains cheese. Suitable for vegetarians"}
{"pizza":"hawaiian", "price":9.99, "Details":"Contains meat. Not suitable for vegetarians"}         

Next, we will attempt to load the data into a Python object using json.loads():

import json

fi = open('sample.json','r')
pizzaJson = json.loads(fi.read())
print(pizzaJson)

Let’s run the code to see the result:

JSONDecodeError: Extra data: line 2 column 1 (char 92)

Our code throws the JSONDecodeError because the records in the JSON file are in an incorrect format. The json.loads() method is only able to read one JSON string at a time.

Solution #1: Reformat the JSON file

We can reformat the JSON file so that the records are in a list with a key pizzas. Let’s look at the revised JSON file:

{"pizzas":
[
{"pizza":"margherita", "price":7.99, "Details":"Contains cheese. Suitable for vegetarians"},
      {"pizza":"pepperoni", "price":9.99, "Details":"Contains meat. Not suitable for vegetarians"},
      {"pizza":"marinara", "price":6.99, "Details":"Dairy free. Suitable for vegetarians."},
      {"pizza":"four cheese", "price":10.99, "Details":"Contains cheese. Suitable for vegetarians"},
      {"pizza":"hawaiian", "price":9.99, "Details":"Contains meat. Not suitable for vegetarians"}
   ]
}

The code from the example does not need to change.

import json

fi = open('sample.json','r')
pizzaJson = json.loads(fi.read())
print(pizzaJson)
print(type(pizzaJson))

Let’s run the code to see the result:

{'pizzas': [{'pizza': 'margherita', 'price': 7.99, 'Details': 'Contains cheese. Suitable for vegetarians'}, {'pizza': 'pepperoni', 'price': 9.99, 'Details': 'Contains meat. Not suitable for vegetarians'}, {'pizza': 'marinara', 'price': 6.99, 'Details': 'Dairy free. Suitable for vegetarians.'}, {'pizza': 'four cheese', 'price': 10.99, 'Details': 'Contains cheese. Suitable for vegetarians'}, {'pizza': 'hawaiian', 'price': 9.99, 'Details': 'Contains meat. Not suitable for vegetarians'}]}
<class 'dict'>

We successfully loaded the JSON data into a Python dictionary. If we want to access the individual records we can use the key pizzas with the pizzaJson dictionary.

records = pizzaJson['pizzas']

for pizza in records:

    print(pizza)
{'pizza': 'margherita', 'price': 7.99, 'Details': 'Contains cheese. Suitable for vegetarians'}
{'pizza': 'pepperoni', 'price': 9.99, 'Details': 'Contains meat. Not suitable for vegetarians'}
{'pizza': 'marinara', 'price': 6.99, 'Details': 'Dairy free. Suitable for vegetarians.'}
{'pizza': 'four cheese', 'price': 10.99, 'Details': 'Contains cheese. Suitable for vegetarians'}
{'pizza': 'hawaiian', 'price': 9.99, 'Details': 'Contains meat. Not suitable for vegetarians'}

Solution #2: Use List Comprehension with json.loads()

The second way we can solve this error is to read the JSON file line by line and the JSON string on each line to the json.loads() method. The JSON file is in the original format:

{"pizza":"margherita", "price":7.99, "Details":"Contains cheese. Suitable for vegetarians"}
{"pizza":"pepperoni", "price":9.99, "Details":"Contains meat. Not suitable for vegetarians"}
{"pizza":"marinara", "price":6.99, "Details":"Dairy free. Suitable for vegetarians."}
{"pizza":"four cheese", "price":10.99, "Details":"Contains cheese. Suitable for vegetarians"}
{"pizza":"hawaiian", "price":9.99, "Details":"Contains meat. Not suitable for vegetarians"}         

We can write the command in one line of code using list comprehension. Let’s look at the revised code:

import json

pizzaJson = [json.loads(line) for line in open('sample.json','r')]

print(pizzaJson)

print(type(pizzaJson))

Let’s run the code to get the result:

[{'pizza': 'margherita', 'price': 7.99, 'Details': 'Contains cheese. Suitable for vegetarians'}, {'pizza': 'pepperoni', 'price': 9.99, 'Details': 'Contains meat. Not suitable for vegetarians'}, {'pizza': 'marinara', 'price': 6.99, 'Details': 'Dairy free. Suitable for vegetarians.'}, {'pizza': 'four cheese', 'price': 10.99, 'Details': 'Contains cheese. Suitable for vegetarians'}, {'pizza': 'hawaiian', 'price': 9.99, 'Details': 'Contains meat. Not suitable for vegetarians'}]
<class 'list'>

We successfully loaded the JSON strings into a list.

Summary

Congratulations on reading to the end of this tutorial! The JSONDecodeError: Extra data occurs when the JSON file contains multiple JSON strings that are in an incorrect format. If you have multiple JSON strings, they should be in a list as a value in a dictionary, with an appropriate key. Otherwise, you can read the JSON strings in the file line by line using a for loop or list comprehension.

For further reading on errors involving JSON, go to the articles:

  • How to Solve Python ValueError: Trailing data
  • How to Solve Python JSONDecodeError: Expecting value: line 1 column 1 (char 0)
  • How to Solve Python TypeError: the JSON object must be str, bytes or bytearray, not ‘TextIOWrapper’

Have fun and happy researching!

Я новичок в DRF, поэтому, пожалуйста, прости меня, если это глупо, но это мой файл JSON:

"articles": [
        {
            "source": {
                "id": "techcrunch",
                "name": "TechCrunch"
            },
            "author": "Jonathan Shieber",
            "title": "All tulips must wilt",
            "description": "It’s a bad day in the world of cryptocurrency. After recovering some during the summer, the value of bitcoin and other cryptocurrencies are sharply down over the last several weeks. Looking back a month, bitcoin was worth around $8,500 a coin. Today it’s wort…",
            "url": "http://techcrunch.com/2019/12/18/all-tulips-must-wilt/",
            "urlToImage": "https://techcrunch.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-18-at-9.39.21-AM.png?w=669",
            "publishedAt": "2019-12-18T15:17:17Z",
            "content": "It’s a bad day in the world of cryptocurrency. After recovering some during the summer, the value of bitcoin and other cryptocurrencies are sharply down over the last several weeks. Looking back a month, bitcoin was worth around $8,500 a coin. Today it’s wort… [+673 chars]"
        },
        {
            "source": {
                "id": "mashable",
                "name": "Mashable"
            },
            "author": "Stan Schroeder",
            "title": "Bitcoin whale moves $1.1 billion in bitcoins for an $80 fee",
            "description": "Bitcoin hasn't (yet) fulfilled its original promise of being widely-used electronic cash, but it still offers some features that would be hard to achieve within the traditional banking system. Namely, moving $1.1 billion from one address to another, in a sing…",
            "url": "https://mashable.com/article/bitcoin-1-1-billion-transaction/",
            "urlToImage": "https://mondrian.mashable.com/2020%252F01%252F15%252F38%252Fd26e834787934c56a33fdeb39faa0be8.84e34.jpg%252F1200x630.jpg?signature=IHj6xz7nTFxvmjn6XOvUiHKJCIM=",
            "publishedAt": "2020-01-15T09:10:59Z",
            "content": "Bitcoin hasn't (yet) fulfilled its original promise of being widely-used electronic cash, but it still offers some features that would be hard to achieve within the traditional banking system. Namely, moving $1.1 billion from one address to another, in a sing… [+1589 chars]"
        },

    ]

И это мои views.py

@api_view(['GET','POST'])
def index(request):
    if(request.method == 'POST'):
        print(request.data)
        return Response({"message": "Got some data!"})
    else:
        message = 'This is a testing message'
        return Response(data = message, status=status.HTTP_200_OK)

Ошибка, которую я получаю:

{
    "detail": "JSON parse error - Extra data: line 1 column 11 (char 10)"
}

Пожалуйста, помогите мне

1 ответ

Лучший ответ

Вместо использования request.data попробуйте

req_body = json.loads(request.body.decode('utf-8'))

И тогда вы можете использовать req_body как dict в python


1

Maede
16 Янв 2020 в 23:36

You are here because when you try to load and parse a JSON file with multiple JSON objects in Python, you received an error. json.decoder.JSONDecodeError: Extra data error. The reason is that the json.load() method can only handle a single JSON object.

The file is invalid if it contains more than one JSON object. When you try to load and parse a JSON file with multiple JSON objects, each line contains valid JSON, but as a whole, it is not a valid JSON as there is no top-level list or object definition. We can call JSON a valid JSON only when there is a top-level list or object definition.

For example, you wanted to read the following JSON file, filter some data, and store it into a new JSON file.

{"id": 1, "name": "Ault", "class": 8, "email": "ault@pynative.com"}
{"id": 2, "name": "john", "class": 8, "email": "jhon@pynative.com"}
{"id": 3, "name": "josh", "class": 8, "email": "josh@pynative.com"}
{"id": 4, "name": "emma", "class": 8, "email": "emma@pynative.com"}

If your file contains a list of JSON objects, and you want to decode one object one-at-a-time, we can do it. To Load and parse a JSON file with multiple JSON objects we need to follow below steps:

  • Create an empty list called jsonList
  • Read the file line by line because each line contains valid JSON. i.e., read one JSON object at a time.
  • Convert each JSON object into Python dict using a json.loads()
  • Save this dictionary into a list called result jsonList.

Let’ see the example now.

import json

studentsList = []
print("Started Reading JSON file which contains multiple JSON document")
with open('students.txt') as f:
    for jsonObj in f:
        studentDict = json.loads(jsonObj)
        studentsList.append(studentDict)

print("Printing each JSON Decoded Object")
for student in studentsList:
    print(student["id"], student["name"], student["class"], student["email"])

Output:

Started Reading JSON file which contains multiple JSON document
Printing each JSON Decoded Object
1 Ault 8 ault@pynative.com
2 john 8 jhon@pynative.com
3 josh 8 josh@pynative.com
4 emma 8 emma@pynative.com

So What Do You Think?

I want to hear from you. What do you think of this article? Or maybe I missed one of the ways to Parse multiple JSON objects from a file, Either way, let me know by leaving a comment below.

Also, try to solve the Python JSON Exercise to have a better understanding of Working with JSON Data in Python.

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 10 questions
  • Each Quiz contains 12-15 MCQ

Сообщение

SyntaxError: JSON.parse: unterminated string literal
SyntaxError: JSON.parse: bad control character in string literal
SyntaxError: JSON.parse: bad character in string literal
SyntaxError: JSON.parse: bad Unicode escape
SyntaxError: JSON.parse: bad escape character
SyntaxError: JSON.parse: unterminated string
SyntaxError: JSON.parse: no number after minus sign
SyntaxError: JSON.parse: unexpected non-digit
SyntaxError: JSON.parse: missing digits after decimal point
SyntaxError: JSON.parse: unterminated fractional number
SyntaxError: JSON.parse: missing digits after exponent indicator
SyntaxError: JSON.parse: missing digits after exponent sign
SyntaxError: JSON.parse: exponent part is missing a number
SyntaxError: JSON.parse: unexpected end of data
SyntaxError: JSON.parse: unexpected keyword
SyntaxError: JSON.parse: unexpected character
SyntaxError: JSON.parse: end of data while reading object contents
SyntaxError: JSON.parse: expected property name or '}'
SyntaxError: JSON.parse: end of data when ',' or ']' was expected
SyntaxError: JSON.parse: expected ',' or ']' after array element
SyntaxError: JSON.parse: end of data when property name was expected
SyntaxError: JSON.parse: expected double-quoted property name
SyntaxError: JSON.parse: end of data after property name when ':' was expected
SyntaxError: JSON.parse: expected ':' after property name in object
SyntaxError: JSON.parse: end of data after property value in object
SyntaxError: JSON.parse: expected ',' or '}' after property value in object
SyntaxError: JSON.parse: expected ',' or '}' after property-value pair in object literal
SyntaxError: JSON.parse: property names must be double-quoted strings
SyntaxError: JSON.parse: expected property name or '}'
SyntaxError: JSON.parse: unexpected character
SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data

Тип ошибки

Что пошло не так?

JSON.parse() обрабатывает (парсит) строку в формате JSON. Это строка должна соответствовать формату, иначе будет выведена ошибка, что был нарушен синтаксис.

Examples

JSON.parse() не допускает запятые

Метод JSON.parse() не разрешает использование, так называемых, trailling запятых.

Обе строки выдадут ошибку типа SyntaxError:

JSON.parse('[1, 2, 3, 4,]');
JSON.parse('{"foo": 1,}');
// SyntaxError JSON.parse: unexpected character
// at line 1 column 14 of the JSON data

Необходимо убрать последние запятые в строках и тогда ошибки не будет:

JSON.parse('[1, 2, 3, 4]');
JSON.parse('{"foo": 1}');

Названия свойств должны быть в двойных кавычках

Вы не можете использовать одинарные кавычки в именах свойств. Например, ‘foo’.

JSON.parse("{'foo': 1}");
// SyntaxError: JSON.parse: expected property name or '}'
// at line 1 column 2 of the JSON data

Вместо этого необходимо написать «foo»:

JSON.parse('{"foo": 1}');

Незначащие нули или плавающая точка без последующей цифры

Вы не можете использовать незначащие нули, например, 01. Плавающая точка должна всегда сопровождаться хотя бы одной цифрой после неё.

JSON.parse('{"foo": 01}');
// SyntaxError: JSON.parse: expected ',' or '}' after property value
// in object at line 1 column 2 of the JSON data

JSON.parse('{"foo": 1.}');
// SyntaxError: JSON.parse: unterminated fractional number
// at line 1 column 2 of the JSON data

Вместо этого напишите просто 1 без нуля и используйте хотя бы одну цифру после точки:

JSON.parse('{"foo": 1}');
JSON.parse('{"foo": 1.0}');

Смотрите также

Понравилась статья? Поделить с друзьями:
  • Json parse array error
  • Json logic error
  • Json load memory error
  • Json last error msg syntax error
  • Json error битрикс