What does "top-level value" mean when parsing JSON?

15 hours ago 2
ARTICLE AD BOX

I'm trying to unmarshal a JSON string into a Go struct, but I keep getting the error:

invalid character '}' after top-level value

Here is my code to reproduce this error:

package main import ( "encoding/json" "fmt" ) type Config struct { Name string `json:"name"` Port int `json:"port"` } func main() { data := `{"name": "server", "port": 8080}}` // note: extra closing brace var cfg Config err := json.Unmarshal([]byte(data), &cfg) if err != nil { fmt.Println("Error:", err) return } fmt.Printf("Parsed: %+v\n", cfg) }

When I run this, it prints:

Error: invalid character '}' after top-level value

I know the JSON is malformed (there's an extra } at the end), but I’m confused about what this specific error message means.

My question:

What does “after top-level value” refer to in this context, and why does the parser complain about the extra } in this way?

I’ve read the Go JSON documentation but couldn’t find details about this error pattern.

Read Entire Article