The Error: unexpected end of json input occurs when the “JSON being parsed is either empty or truncated.”
To fix the Error: unexpected end of json input in Go, ensure the JSON input is complete and well-formed.
package main
import (
"encoding/json"
"fmt"
)
type Student struct {
Name string `json:"name"`
Age int `json:"age"`
}
func main() {
var s Student
jsonData := `{"name":"Krunal", "age": 30`
err := json.Unmarshal([]byte(jsonData), &s)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Printf("Person: %+v\n", s)
}
Output
Error: unexpected end of JSON input
In this example, the jsonData variable contains an incomplete JSON string (missing the closing }), which would result in the “unexpected end of JSON input” error.
You can fix the error by correcting the JSON input.
jsonData := `{"name":"Krunal", "age": 30}`
In a real-world scenario, the JSON input might come from an external source like a file or an API response.
You should always validate the JSON input before attempting to unmarshal it.
If you presume the JSON input might be empty, you can add a check before calling the json.Unmarshal() function.
if len(jsonData) == 0 {
fmt.Println("Error: Empty JSON input")
return
}
That’s it!
Related posts
json: cannot unmarshal object into Go struct field
Lowercase JSON Key Names with JSON Marshal in Go

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.