recis recis - 1 year ago 233
JSON Question

Go JSON into Map

I have a really simple JSON file, something like this, but with thousands of strings:


and I want to load the fruits into a


What is the best method? Is there a way where I don't need to iterate over each element and insert into the map using a loop?

Thank you,

Answer Source

here is example how you can Unmarshal to string list without any struct.

package main

import "fmt"
import "encoding/json"

func main() {
    src_json := []byte(`{"fruits":["apple","banana","cherry","date"]}`)
    var m map[string][]string
    err := json.Unmarshal(src_json, &m)
    if err != nil {
    fmt.Printf("%v", m["fruits"][0]) //apple

Or instead of String list you can use map[string][]interface{}

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download