15

In Go, I have some JSON from third-party API and then I'm trying to parse it:

b := []byte(`{"age":21,"married":true}`)
var response_hash map[string]string
_ = json.Unmarshal(b, &response_hash)

But it fails (response_hash becomes empty) and Unmarshal can handle only JSON with quotes:

b := []byte(`{"age":"21","married":"true"}`)
var response_hash map[string]string
_ = json.Unmarshal(b, &response_hash)

Is there any way to read the first version of JSON from third-party API?

1
  • 3
    21 and married? You're making me feel rushed over here Commented Apr 1, 2014 at 17:56

1 Answer 1

23

Go is a strongly typed language. You need to specify what types the JSON encoder is to expect. You're creating a map of string values but your two json values are an integer and a boolean value.

Here's a working example with a struct.

http://play.golang.org/p/oI1JD1UUhu

package main

import (
    "fmt"
    "encoding/json"
    )

type user struct {
    Age int
    Married bool
}

func main() {
    src_json := []byte(`{"age":21,"married":true}`)
    u := user{}
    err := json.Unmarshal(src_json, &u)
    if err != nil {
        panic(err)
    }
    fmt.Printf("Age: %d\n", u.Age)
    fmt.Printf("Married: %v\n", u.Married)
}

If you want to do it in a more dynamic manner you can use a map of interface{} values. You just have to do type assertions when you use the values.

http://play.golang.org/p/zmT3sPimZC

package main

import (
    "fmt"
    "encoding/json"
    )

func main() {
    src_json := []byte(`{"age":21,"married":true}`)
    // Map of interfaces can receive any value types
    u := map[string]interface{}{}
    err := json.Unmarshal(src_json, &u)
    if err != nil {
        panic(err)
    }
    // Type assert values
    // Unmarshal stores "age" as a float even though it's an int.
    fmt.Printf("Age: %1.0f\n", u["age"].(float64))
    fmt.Printf("Married: %v\n", u["married"].(bool))
}
Sign up to request clarification or add additional context in comments.

2 Comments

Also, don't forget, if you want to use different variable names for the struct, you can interface with json vars using `json:"valuename"` play.golang.org/p/8QD2yfb-fy
I'd just add that this blog post goes into excellent detail on using the second approach: blog.golang.org/2011/01/json-and-go.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.