Unterminated string in JSON: How to Fix It in Go
A string in your JSON opens with a quote but never closes. Learn what Go's encoding/json reports instead, the usual causes, and how to fix and guard…
Have broken JSON right now? Fix it free in under 1 second — no signup.
Fix My JSON →A JSON string opened with a double quote and the parser reached a newline or the end of the input before it found the closing quote. In Go, encoding/json reports this as "unexpected end of JSON input" or "invalid character '\n' in string literal", and other parsers call it "unterminated string".
What “Unterminated string in JSON” means
The parser found a string that never closes. A JSON string starts with a double quote and must end with a matching double quote, and the standard does not allow a raw line break inside it. Go's encoding/json does not print the words "unterminated string". It says "unexpected end of JSON input" when the data ends inside the string, and "invalid character '\n' in string literal" when a raw newline shows up first. Other Go JSON libraries and editors use the "unterminated string" wording for the same fault.
Causes and the fix for each
The closing quote is missing
Someone typed or generated the opening quote and forgot the closing one.
Before (fails):{"name": "Ada}
After (valid):
{"name": "Ada"}
A double quote inside the value is not escaped
The inner quote ends the string early, and the rest of the text no longer parses. The quote must be written as \" inside a string.
Before (fails):{"quote": "She said "hi" and left}
After (valid):
{"quote": "She said \"hi\" and left"}
A raw line break sits inside the string
The standard requires control characters, including newline, to be escaped as \n. A literal line break in the string is invalid.
Before (fails):{"note": "line one
line two"}
After (valid):
{"note": "line one\nline two"}
A trailing backslash escapes the closing quote
Windows paths are a common case. The final backslash turns the closing quote into a literal character, so the string never ends.
Before (fails):{"path": "C:\temp\"}
After (valid):
{"path": "C:\\temp\\"}
The input was cut off before the string finished
A short read, a size limit, a closed connection or a truncated model response can stop the text in the middle of a value.
Before (fails):{"id": 7, "title": "Quarterly rep
After (valid):
{"id": 7, "title": "Quarterly report"}
How to fix it in Go
- Print the raw bytes you passed to json.Unmarshal, with %q, before you change any code. This shows whether the text is cut off, has a raw newline, or has a stray quote.
- Paste the text into the Validator to see the line and column of the first problem. If the input is cut off, the error points at the end of the text.
- Look at the string where the error points. Add the missing closing quote, escape inner quotes as \", and replace raw line breaks with \n.
- If the text comes from a request body, a file or a stream, check that you read all of it. Use io.ReadAll on the full body and compare the length with Content-Length when the server sends one.
- If the JSON is built by hand with string concatenation or fmt.Sprintf, stop doing that. Build a struct or map and call json.Marshal, which escapes quotes, backslashes and newlines for you.
- If the text is machine output you cannot fix at the source, run it through the Repair tool to close the string and the open brackets, then check the result with the Validator.
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
)
// parse decodes data into v and reports the line and column of a syntax error.
func parse(data []byte, v any) error {
err := json.Unmarshal(data, v)
if err == nil {
return nil
}
var se *json.SyntaxError
if errors.As(err, &se) {
off := int(se.Offset)
if off > len(data) {
off = len(data)
}
line := bytes.Count(data[:off], []byte("\n")) + 1
col := off - (bytes.LastIndexByte(data[:off], '\n') + 1)
return fmt.Errorf("bad JSON at line %d, column %d: %w", line, col, err)
}
return err
}
func main() {
var out map[string]any
data := []byte("{\n \"name\": \"Ada\n}")
if err := parse(data, &out); err != nil {
fmt.Printf("%v\nraw: %q\n", err, data)
}
}
Variants of the message
| Message | Cause | Fix |
|---|---|---|
| unexpected end of JSON input | The text ended inside a string or before a value was complete. | Check for truncated input and add the missing closing quote and brackets. |
| invalid character '\n' in string literal | A raw newline sits inside a string. | Replace the line break with \n. |
| invalid character 'x' after object key:value pair | An unescaped inner quote ended the string early and text followed it. | Escape the inner quote as \". |
| invalid character 'x' in string escape code | A backslash is followed by a character that is not a valid escape. | Double the backslash as \\ or use a valid escape. |
| unterminated string | Wording used by other parsers and editors for a string with no closing quote. | Add the closing quote and escape quotes and newlines inside the value. |
Related errors
- Unexpected end of JSON input in Go: What It Means
- Invalid JSON Meaning: What It Is and How to Fix It
- Invalid control character in JSON: How to Fix It in Python
- Invalid escape sequence in JSON: Fix JSON.parse() in JavaScript
Key takeaways
- Every JSON string needs an opening and a closing double quote.
- Raw newlines and unescaped inner quotes break a string.
- Go reports this mostly as "unexpected end of JSON input" or "invalid character '\n' in string literal".
- Truncated input is a common source, so check how the data was read.
- Build JSON with json.Marshal and not with string concatenation.
FAQ
Why does Go say "unexpected end of JSON input" and not "unterminated string"?
The standard library reports the first point where the text stops being valid. When the data ends inside a string, it uses that message. The words "unterminated string" come from other parsers.
Can a JSON string span several lines?
No. The standard does not allow a raw line break inside a string. Write the line break as \n.
Does json.Marshal produce unterminated strings?
No. json.Marshal escapes quotes, backslashes and control characters in strings. The problem usually comes from hand-built text or from input that was cut off.
How do I find the exact position of the error?
Use errors.As to get a *json.SyntaxError and read its Offset field. It is the byte count read before the error was found. Convert it to a line and column as in the code above.
Can a tool fix this for me?
For a missing closing quote, a stray inner quote or a cut-off ending, the Repair tool can often produce valid JSON. Check the result, because the repair may guess where the string was meant to end.
Still dealing with broken JSON?
Paste it in and get it fixed in under 1 second — free, no signup, no install. Works with ChatGPT, Claude, n8n, and any AI output.
Fix My JSON Free →The JSON Medic newsletter
One JSON error and its fix, weekly, plus new guides and tools. Free.
We send a confirmation link first. Unsubscribe with one click in any mail. See the privacy policy.
Related Articles