How to Fix: Unexpected End of Json Input in Golang

The unexpected end of JSON input is one of the errors encountered by the Go language users while decoding or parsing the JSON data. In this article, we will understand the reason behind this error. Also, we will see the solution to fix this error.

Reason Behind Unexpected End of Json Input 

This error indicates that the JSON parser reached the end of the input unexpectedly, which means that the JSON data is not valid. Because the input JSON string is incomplete or malformed. Here are some common causes and solutions to fix this error:

1. Incomplete or Malformed JSON Data:

Cause:

The JSON data you are trying to parse is incomplete or not properly formatted.

package main

import (
    "encoding/json"
    "fmt"
)

func main() {

    myJSONdata := `{"employeeName": "Alice", "employeeAge": 25, "employeeCity": "Wonderland"`

    var employeeData map[string]interface{}
    err := json.Unmarshal([]byte(myJSONdata), &employeeData)
    if err != nil {
        fmt.Println("Error decoding JSON:", err)
        return
    }

    fmt.Println("Decoded JSON:", employeeData)
}

Output:

Error decoding JSON: unexpected end of JSON input

On execution of the above Go language code example, an error is encountered which means that the JSON data we are trying to parse is incomplete.

Solution:

Ensure that the JSON data you are working with is valid. Check for missing opening { or closing brackets }, quotes “, or any other syntax errors in the code. In the above example, we can observe that the Malformed JSON data is used. Here, a closing bracket is missing from the myJSONdata string. So just place it and the error will be resolved as shown below.

Output:

Decoded JSON: map[employeeAge:25 employeeCity:Wonderland employeeName:Alice]

2. Empty JSON Data:

Cause:

The JSON input is empty, and the parser is expecting more data.

package main

import (
    "encoding/json"
    "fmt"
)

func main() {

    myJSONdata := `{"employeeName": "Alice", "employeeAge": 25, `

    var employeeData map[string]interface{}
    err := json.Unmarshal([]byte(myJSONdata), &employeeData)
    if err != nil {
        fmt.Println("Error decoding JSON:", err)
        return
    }

    fmt.Println("Decoded JSON:", employeeData)
}

Output:

Error decoding JSON: unexpected end of JSON input

In this example, the same error is encountered because the myJSONdata variable is an empty string, it contains nothing. Here, the error is checked during decoding, and an error message is printed if the JSON data is empty.

Solution:

Make sure that the JSON data that needs to be parsed is not empty. In the above code snippet, the JSON data is intentionally kept empty to show how this error is raised. To fix this error we need to add the JSON data in the myJSONdata variable. Have a look at the below code which runs without any error.

package main

import (
    "encoding/json"
    "fmt"
)

func main() {

    myJSONdata := `{"employeeName": "Alice", "employeeAge": 25, "employeeCity": "Wonderland"`}

    var employeeData map[string]interface{}
    err := json.Unmarshal([]byte(myJSONdata), &employeeData)
    if err != nil {
        fmt.Println("Error decoding JSON:", err)
        return
    }

    fmt.Println("Decoded JSON:", employeeData)
}

Output

Decoded JSON: map[employeeAge:25 employeeName:Alice]

Conclusion:

In conclusion, when working with JSON data in Go, it is important to understand the potential reasons for the unexpected end of JSON input error. This error can arise from various issues, such as malformed, incomplete or empty JSON data. Careful consideration of these factors, along with thorough error handling and validation checks, can help us to prevent this error.

Jerry Richard
Follow me

Was this post helpful?

Yes
No
Thanks for your feedback!

Leave a Comment