Iterate through multiple JSON files and extract value

Hello,

I’m working with elasticsearch_opendistro_ism_policy resource.

I have a directory with JSON files with the content below

JSON policy
{
  "policy": {
    "description": "delete_all_indices_older_30d",
    "default_state": "hot",
    "states": [
        {
            "name": "hot",
            "actions": [],
            "transitions": [
                {
                    "state_name": "delete",
                    "conditions": {
                        "min_index_age": "30d"
                    }
                }
            ]
        },
        {
            "name": "delete",
            "actions": [
                {
                    "delete": {}
                }
            ],
            "transitions": []
        }
    ],
    "ism_template": [
        {
            "index_patterns": [
                "metrics-*",
            ],
            "priority": 200
        }
    ]
  }
}

My goal is to extract the description value from all JSON files and assign it to policy_id argument from the tf resource
The same thing should be done with the body argument, but there I need to place the content of the JSON file

Can someone help to resolve it ?

@apparentlymart, Can you please help?

for now, I have locals below, but I don’t know exactly what I should do next

locals {
  json_files = fileset(path.module, "config_files/*.json")
  json_data  = [for f in local.json_files : jsondecode(file("${path.module}/${f}"))]
}

Hi @menvol3,

Mentioning me here doesn’t do anything, since I have notifications turned off for this forum. However, I do still routinely look at the topics here and so I noticed the question anyway regardless of the notification.

It seems like your local.json_data should now be a list of objects loaded from the various JSON files, and so you can read the description attribute from each of them using the splat operator [*]:

local.json_data[*].description
1 Like

Check my multiple YAML and JSON extractor. It will make your life easier:

Cheers
Levon