Error sending an array within a JSON object

309 Views Asked by At

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?

1

There are 1 best solutions below

2
Adrian On BEST ANSWER

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" }},
})