Search code examples
jsongoiris-go

Error sending an array within a JSON object


In order to send this JSON with Iris Go

{
  "response_type": "in_channel",
  "text": "It's 80 degrees right now.",
  "attachments": [
    {
        "text":"Partly cloudy today and tomorrow"
    }
  ]
}

I'm trying with this but is not working

ctx.JSON(iris.Map{
    "text": "It's 80 degrees right now.",
    "response_type": "in_channel",
    "attachments":[{ "text": "Partly cloudy today and tomorrow" }],
})

Because the following error appear in the attachments line

syntax error: unexpected {, expecting expression

Do you know how to fix it?


Solution

  • In your Go code, you don't specify a type for the array or its element. Assuming you want it to be iris.Map:

    ctx.JSON(iris.Map{
        "text": "It's 80 degrees right now.",
        "response_type": "in_channel",
        "attachments": []iris.Map{iris.Map{ "text": "Partly cloudy today and tomorrow" }},
    })