13

I'm using the following code without success to parse json value but its inside array []

https://play.golang.org/p/5HdWeyEtvie

package main

import (
    "encoding/json"
    "fmt"
)


var input = `
[
{
    "created_at": "Thu May 31 00:00:01 +0000 2012"
},
{
    "created_at": "Thu May 31 00:00:01 +0000 2012"
}
]
`

func main() {
    var val map[string]interface{}
    if err := json.Unmarshal([]byte(input), &val); err != nil {
        panic(err)
    }
    fmt.Println(val)
}

The idea is to get the value as key and print it , like a function that get string parameter="created_at" and prints it.

1
  • 2
    Try using array of map[string]interface{}
    – dakait
    Commented Nov 29, 2018 at 11:39

4 Answers 4

24

Your input is:

[  
   {  
      "created_at":"Thu May 31 00:00:01 +0000 2012"
   },
   {  
      "created_at":"Thu May 31 00:00:01 +0000 2012"
   }
]

You can turn this into a struct like so:

type MyArray []struct {
    CreatedAt string `json:"created_at"`
}

Now you can read your JSON data and loop through it to get your desired values. Here's what you get:

package main

import (
    "encoding/json"
    "fmt"
)

var str = `[
    {
    "created_at": "Thu May 31 00:00:01 +0000 2012"
    },
    {
    "created_at": "Thu May 31 00:00:01 +0000 2013"
    }
]`

type MyArray struct {
    CreatedAt string `json:"created_at"`
}

func main() {
    var m []MyArray
    if err := json.Unmarshal([]byte(str), &m); err != nil {
        panic(err)
    }
    for _, val := range m {
        fmt.Println(val.CreatedAt)
    }
}

In the future, you could consult JSON-to-Go if you're unsure how to convert a JSON object to a struct -- it's quite useful. Cheers!

2
  • var m []MyArray ... used to parse that array! Thank you
    – Russo
    Commented Aug 28, 2020 at 19:29
  • 1
    Worth noting that type MyArray struct{...} and var m []MyArray work, but type MyArray []struct{...} and var m MyArray work too. But the former way is more clear and idiomatic, in my opinion. Commented Apr 30, 2022 at 16:23
7

You're unmarshaling an array to a map. That obviously won't work. you need val to be an array.

func main() {
    var val []map[string]interface{} // <---- This must be an array to match input
    if err := json.Unmarshal([]byte(input), &val); err != nil {
        panic(err)
    }
    fmt.Println(val)
}
4
  • Thanks but I needs to get it as key, I mean created_at value
    – user4445419
    Commented Nov 29, 2018 at 11:43
  • I'm sorry, what needs to get what as key? Commented Nov 29, 2018 at 11:44
  • think of a function which gets the created_at as parameter and run extract the value
    – user4445419
    Commented Nov 29, 2018 at 11:53
  • I still don't understand your comment. But that sounds unrelated to your original question. If it's part of your question, please update your question to clarify. Commented Nov 29, 2018 at 11:56
4
var input = `[
    {
    "created_at": "Thu May 31 00:00:01 +0000 2012"
    },
    {
    "created_at": "Thu May 31 00:00:01 +0000 2013"
    }
]`

func main() {
    var val []struct {
        CreatedAt string `json:"created_at"`
    }

    if err := json.Unmarshal([]byte(input), &val); err != nil {
        panic(err)
    }

    fmt.Println(val[0].CreatedAt)
}
1
  • val is an array, try fmt.Println(val[0].CreatedAt)
    – CallMeLoki
    Commented Nov 29, 2018 at 12:36
2

You should create a struct like this.

type Data struct {
    CreateAt time.Time `json:"create_at"`
}

func main() {
    var in []Data
    if err := json.Unmarshal([]byte(input), &in); err != nil{
        log.Fatal(err)
    }
    for _, data := range in{
        fmt.Println(data.CreateAt)
    }
}