Using jsonencode & templatefile to loop over sections in json file

I’m trying to loop over some sections in a grafana dashboard but I’m running into some problems.

The section I am trying to loop over is as follows:

  ${jsonencode([
    for id in awsMasterInstanceId:
  {
    "type": "query",
    "query": {
        "params": [
        "${id}",
        "${grafanaAlarms.ebsVolumeUsed.stats.period}",
        "now"
        ]
    },
    "reducer": {
        "type": "${grafanaAlarms.ebsVolumeUsed.stats.type}",
        "params": []
    },
    "evaluator": {
        "type": "${grafanaAlarms.ebsVolumeUsed.threshold.evaluator}",
        "params": [
        ${grafanaAlarms.ebsVolumeUsed.threshold.limit}
        ]
    },
    "operator": {
        "type": "and"
    }
    }
])}

But I am getting a few different errors one of which is Error: status: 422, body: [{"fieldNames":["Dashboard"],"classification":"RequiredError","message":"Required"}]
However, I think this is a red herring as if I remove the jsonencode loop then the dashboard can be applied fine. So I believe I am not formatting this section correctly.

Is there anything I am doing wrong when using jsonencode?

There is not enough context here to be able to advise.

You should change your Terraform code temporarily for testing so you can see the entire generated JSON and compare it to what you expected - for example by assigning it to a local variable and using terraform console to view it.

So it looks like TF is adding a trailing comma which I suspect might be breaking it. I can see this in the plan
After the last + type = "query" there should not be a trailing comma. Do you know of anyway to get round this? Thanks for the reply.

+ panels        = [
    + {
        + alert           = {
            + alertRuleTags       = {}
            + conditions          = [
                + [
                    + {
                        + evaluator = {
                            + params = [
                                + 90,
                            ]
                            + type   = "gt"
                        }
                        + operator  = {
                            + type = "and"
                        }
                        + query     = {
                            + params = [
                                + "i-0c5c3e8a3318940a2",
                                + "5m",
                                + "now",
                            ]
                        }
                        + reducer   = {
                            + params = []
                            + type   = "avg"
                        }
                        + type      = "query"
                    },
                    + {
                        + evaluator = {
                            + params = [
                                + 90,
                            ]
                            + type   = "gt"
                        }
                        + operator  = {
                            + type = "and"
                        }
                        + query     = {
                            + params = [
                                + "i-0bae60e030f7a1b93",
                                + "5m",
                                + "now",
                            ]
                        }
                        + reducer   = {
                            + params = []
                            + type   = "avg"
                        }
                        + type      = "query"
                    },
                ],
            ]

The above appears to be in HCL format, not JSON

Technically you are correct as this is the output of terraform plan and it is rendering the json as HCL.
The question still remains, it is adding a trailing comma which is causing the apply to fail. Is there a way to prevent the trailing comma?

It’s normal and expected for there to be trailing commas in HCL.

If you’re trying to debug the formation of your JSON, you can’t do that by looking at a separate HCL rendering of your data.