ENRU

Extra data after JSON: what “Unexpected non-whitespace character after JSON” means and how to fix it

Updated

The document ended. Something kept talking. A JSON text is exactly one value, and whatever follows it, a second object, a stray bracket or a line of log, makes the whole text invalid.

Extra data after the closing bracket

Two shapes cover almost every case. One closing bracket too many, usually after a hand edit:

{"id": 1, "status": "ok"}}

Or several complete documents in one text: a log in JSON Lines format, two responses written into one buffer, a file appended to instead of rewritten:

{"id": 1, "status": "ok"}
{"id": 2, "status": "failed"}

For the first, delete the extra bracket (or find the opening one that is missing further up). For the second, decide what the data is. If it is one document with several records, wrap them in an array:

[{"id": 1, "status": "ok"}, {"id": 2, "status": "failed"}]

If it is a stream of records, keep it as JSON Lines and read it line by line. One more cause is easy to miss: a file overwritten in place with shorter content keeps the tail of the old version after the new closing bracket.

Why JSON allows only one value (RFC 8259)

RFC 8259, section 2: JSON-text = ws value ws. One value, with optional whitespace before and after. A trailing line break is fine; a second value, a comment or a single extra bracket is not. Formats built on top of JSON, such as JSON Lines, define their own framing (one value per line), and their files are not JSON documents as a whole.

Extra data messages by parser

Input: {"a":1} {"b":2}, the second object starting at character index 8. Messages come from running each parser on 2026-11-08; Firefox’s from the SpiderMonkey source. The last column matters as much as the message: some parsers don’t treat extra data as an error at all.

ParserMessageBy default
Chrome 156, Node 24–25 (V8)Unexpected non-whitespace character after JSON at position 8 (line 1 column 9)Error
Firefox (SpiderMonkey)unexpected non-whitespace character after JSON dataError
Safari 27 (JavaScriptCore)JSON Parse error: Unable to parse JSON stringError, with no hint of the cause
Python 3.9–3.14Extra data: line 1 column 9 (char 8)Error; raw_decode reads one value and says where it stopped
Go 1.25–1.27: json.Unmarshalinvalid character '{' after top-level valueError; json.Decoder reads values one after another
Java, Jackson 2.15–2.23No errorReturns the first value and ignores the rest
C#, System.Text.Json 10'{' is invalid after a single JSON value. Expected end of data.Error
C#, Newtonsoft.Json 13Additional text encountered after finished reading JSON content: {. Path '', line 1, position 8.Error
PHP 8.3Syntax errorError
Rust, serde_json 1.0trailing characters at line 1 column 9Error
jq 1.7.1No errorjq reads a stream and prints both values; {"a":1}} gives Unmatched '}' at line 1, column 8
MySQL 8.4: JSON columnInvalid JSON text: "The document root must not be followed by other values." at position 8Error
JSON Statham ValidatorExtra data after JSON, line 1, column 9Error, code extra_data

Jackson can be made strict with a deserialization feature; its message then reads:

Trailing token (of type START_OBJECT) found after value (bound as `com.fasterxml.jackson.databind.JsonNode`): not allowed as per `DeserializationFeature.FAIL_ON_TRAILING_TOKENS`

How to read several JSON values from one text

If the extra data is a mistake, delete it. If the text really holds several values, read them as such: line by line for JSON Lines, value by value for concatenated JSON. All snippets were run on two-record input.

JavaScript: JSON Lines

function parseJsonLines(text) {
  return text
    .split('\n')
    .filter((line) => line.trim() !== '')
    .map((line) => JSON.parse(line));
}

Python: JSON Lines and concatenated values

import json


def parse_json_lines(text):
    return [json.loads(line) for line in text.splitlines() if line.strip()]


def parse_concatenated(text):
    decoder = json.JSONDecoder()
    values, pos = [], 0
    while True:
        while pos < len(text) and text[pos].isspace():
            pos += 1
        if pos == len(text):
            return values
        value, pos = decoder.raw_decode(text, pos)
        values.append(value)

raw_decode returns the value and the index where it ended, so the loop works even without line breaks: {"a":1}{"b":2} [3] gives three values.

Go: json.Decoder reads a stream

import (
	"encoding/json"
	"errors"
	"io"
	"strings"
)

func decodeAll(input string) ([]any, error) {
	dec := json.NewDecoder(strings.NewReader(input))
	var values []any
	for {
		var v any
		err := dec.Decode(&v)
		if errors.Is(err, io.EOF) {
			return values, nil
		}
		if err != nil {
			return values, err
		}
		values = append(values, v)
	}
}

Java (Jackson)

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.List;

static List<JsonNode> readAll(ObjectMapper mapper, String text) throws IOException {
    try (MappingIterator<JsonNode> it = mapper.readerFor(JsonNode.class).readValues(text)) {
        return it.readAll();
    }
}

C#

using System.Text.Json.Nodes;

static List<JsonNode?> ParseJsonLines(string text) =>
    text.Split('\n')
        .Where(line => line.Trim().Length > 0)
        .Select(line => JsonNode.Parse(line))
        .ToList();

PHP

function parse_json_lines(string $text): array
{
    $values = [];
    foreach (explode("\n", $text) as $line) {
        if (trim($line) !== '') {
            $values[] = json_decode($line, true, 512, JSON_THROW_ON_ERROR);
        }
    }
    return $values;
}

How to stop extra data from appearing

  • Name the format. A .json file holds one document; .jsonl and .ndjson hold one per line. Appending a record to a .json file breaks it; appending to JSON Lines is the whole point.
  • Rewrite, don’t overwrite in place. Open files for writing with truncation, or write a temporary file and rename it, so a shorter document can’t leave the old tail behind.
  • Format after hand edits. In a pretty-printed document an extra } sits visibly out of line, and the Formatter won’t format until it is gone.
  • Make lenient readers strict in tests. Jackson and jq accept trailing data; a strict check in CI catches what they let through.

Find extra data in the validator

Try it: open the extra-data sample in the Validator. The card names the first character after the finished value and its line and column; everything from there to the end is the extra part. A comment after the closing bracket, {} // note, is reported the same way: once the value is complete, the validator expects only whitespace.

Extra data in JSON: FAQ

What does “Extra data” mean in Python?

json.loads read one complete value and found more text after it, at the line and column given. Usually it is a JSON Lines file or two documents glued together: read line by line, or with raw_decode.

Can a JSON file contain several objects?

Not side by side. Put them into an array, [{…}, {…}], or use JSON Lines, one object per line, which is a separate format with its own readers.

Why doesn’t Jackson report extra data?

ObjectMapper stops after the first value and ignores the rest by default. Enable DeserializationFeature.FAIL_ON_TRAILING_TOKENS to get an error.

Is a line break after the JSON allowed?

Yes. Spaces, tabs and line breaks may follow the value, so a trailing newline is fine. Anything else is extra data.