Unclosed string in JSON: “Unterminated string in JSON at position N”
Updated
A string with no end. Not on my watch. A JSON string runs from one straight double quote to the next unescaped one; if the input ends first, the parser has read everything after the opening quote as text and has nothing left to close it.
The error and the fix
The most common unclosed string doesn’t look unclosed at all. A Windows path that ends with a backslash:
{"path": "C:\temp\"}In JSON a backslash starts an escape, and \" is an escaped quote, a quote character inside the string. So the string doesn’t end there; it swallows "} and runs into the end of the input. Every backslash in a path has to be doubled:
{"path": "C:\\temp\\"}The broken version hides a second problem: \t is a valid escape for a tab, so even with the closing quote fixed, "C:\temp" silently means “C:” + tab + “emp”. Valid JSON, wrong data.
Why the parser can’t find the end
RFC 8259, section 7: “A string begins and ends with quotation marks.” Inside, the quotation mark, the backslash and control characters must be escaped, and \" is how a quote gets into a string. A parser reads characters until it meets a quote that is not escaped. Nothing else can end a string: not a line break, not a bracket, not the end of a line in a file.
Unterminated string error messages by parser
A string cut at the end, {"a": "b, and the path above, {"path": "C:\temp\"}. Only Python and this site’s validator point at the opening quote; the rest report the end of the input.
| Parser | Cut off: {"a": "b | Trailing backslash: {"path": "C:\temp\"} |
|---|---|---|
| Chrome, Edge, Node.js (V8) | Unterminated string in JSON at position 8 (line 1 column 9) | Unterminated string in JSON at position 20 (line 1 column 21) |
| Node.js 18, older Chrome (V8 10.2) | Unexpected end of JSON input | Unexpected end of JSON input |
| Firefox | JSON.parse: unterminated string at line 1 column 9 of the JSON data | JSON.parse: unterminated string at line 1 column 21 of the JSON data |
| Safari | JSON Parse error: Unterminated string | JSON Parse error: Unterminated string |
| Python | Unterminated string starting at: line 1 column 7 (char 6) | Unterminated string starting at: line 1 column 10 (char 9) |
| Go (encoding/json) | unexpected end of JSON input | unexpected end of JSON input |
| Java (Jackson 2) | Unexpected end-of-input: was expecting closing quote for a string value | Unexpected end-of-input: was expecting closing quote for a string value |
| C# (System.Text.Json) | Expected end of string, but instead reached end of data. | Expected end of string, but instead reached end of data. |
| C# (Newtonsoft.Json) | Unterminated string. Expected delimiter: ". Path 'a', line 1, position 8. | Unterminated string. Expected delimiter: ". Path 'path', line 1, position 20. |
| PHP (json_decode) | Control character error, possibly incorrectly encoded, json_last_error() 3 | Control character error, possibly incorrectly encoded, json_last_error() 3 |
| Rust (serde_json) | EOF while parsing a string at line 1 column 8 | EOF while parsing a string at line 1 column 20 |
| jq | jq: parse error: Unfinished string at EOF at line 1, column 8 | jq: parse error: Unfinished string at EOF at line 1, column 20 |
| RapidJSON | Missing a closing quotation mark in string. | Missing a closing quotation mark in string. |
PHP’s “control character error” is the odd one: its scanner treats the end of the input inside a string like a forbidden control character, so the message points to encoding while the cause is a missing quote. Checked by running: Node.js 18, 24 and 25.1, Safari 27, Python 3.9 and 3.14, Go 1.25–1.27, Jackson 2.22 and 3.2, System.Text.Json and Newtonsoft.Json 13.0.4, PHP 8.3 and 8.5, serde_json 1.0, jq 1.7 and 1.8. Firefox and RapidJSON: from their source code; Firefox’s messages are also listed on Mozilla’s reference pages.
Where unclosed strings come from
- A backslash before the closing quote: Windows paths, regular expressions, anything typed by hand with
\at the end. - A curly closing quote:
"done”opens with a straight quote and “closes” with”, which is just another character. See smart quotes. - Cut-off input: a response, a log line or a copy that stopped in the middle of a string.
- A raw line break inside a string. Most parsers call it a control character error, but Safari, Newtonsoft.Json and jq report an unterminated string for
"b+ line break.
In the middle of a document a trailing backslash causes a different error further on: in {"path": "C:\temp\", "user": "Owl"} the string closes at the quote before user, and the parser then stumbles over the word user. If an error points at a word that should be a key, look for a backslash before the previous quote.
How to escape strings correctly in code
Serializers escape backslashes and quotes for you; the problem appears only when JSON is typed or assembled by hand. Each snippet writes the path from the example as valid JSON.
JavaScript
const path = "C:\\temp\\"; // the path C:\temp\, backslashes doubled in source code
console.log(JSON.stringify({ path })); // {"path":"C:\\temp\\"}Python
import json
# Typed by hand, \t silently became a tab:
print("\t" in json.loads(r'{"path": "C:\temp"}')["path"]) # True
print(json.dumps({"path": "C:\\temp\\"})) # {"path": "C:\\temp\\"}Go
package main
import (
"encoding/json"
"fmt"
)
func main() {
b, _ := json.Marshal(map[string]string{"path": `C:\temp\`})
fmt.Println(string(b)) // {"path":"C:\\temp\\"}
}Java (Jackson)
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
String json = new ObjectMapper().writeValueAsString(Map.of("path", "C:\\temp\\"));
System.out.println(json); // {"path":"C:\\temp\\"}C#
using System.Text.Json;
Console.WriteLine(JsonSerializer.Serialize(new { path = @"C:\temp\" })); // {"path":"C:\\temp\\"}PHP
$path = 'C:\temp\\'; // single quotes: only \\ needs care
echo json_encode(['path' => $path]); // {"path":"C:\\temp\\"}How to keep strings closed
- Never put paths, regular expressions or user text into JSON by string concatenation; let the serializer escape them.
- In configs written by hand, use forward slashes for Windows paths:
"C:/temp/"works in almost every Windows API and needs no escaping. - Keep JSON away from word processors and chat apps that swap in curly quotes.
- Check edited files in the JSON validator: it shows where the unclosed string starts.
Try it in the validator
Open a sample with an unclosed string in the JSON validator. It reports unclosed_string at the end of the input, adds “Opened at line L, column C” for the quote that started the string, and suggests doubling a trailing backslash.
Related: invalid escape ("C:\Users"), control character, smart quotes, unclosed bracket, and all JSON errors.
Unterminated string FAQ
What does “Unterminated string in JSON at position” mean?
A string was opened with " and the input ended before the closing ". The position is the end of the input; the opening quote is earlier, often much earlier.
Why does "C:\temp\" break JSON?
The last \" is an escaped quote, so the string never closes. Write each backslash twice: "C:\\temp\\", or use forward slashes.
Can a JSON string span several lines?
Not with real line breaks: those must be written as \n. A raw line break inside a string is a control character error in most parsers.
Why does PHP say “Control character error” for a missing quote?
Its parser treats the end of the input inside a string like a control character. json_last_error() returns 3, but the fix is the same: close the string.