Output of "terraform output -json"

Hi,

I have the need of postprocessing the output of terraform output --json. While running that I noticed that it is “noisier” than expected.
I’m only interested in the value of the value key which would be more aligned with a json like .tf.json. Wouldn’t that be an option terraform could provide out-of-the box?
I’ll look into filtering it with jq for now.

The two valid methods I know of are below… maybe using bith could target just the item you want for less output clutter…

‘terraform output instance public ip’

List a specific declared output

‘terraform output -json’

List all outputs in JSON format

As I mentioned output ofterraform output -json is a bit noisy with a lot of extra stuff and also output values are in the value key instead of being the “.value” of the output variable key.

There are no plans to change the format of terraform output -json, as we consider it stable. The additional information is important for some users, and easily filtered using jq, which is our recommended approach:

$ terraform output -json
{
  "animal": {
    "sensitive": true,
    "type": "string",
    "value": "horse"
  }
}
$ terraform output -json | jq -r .animal.value
horse

Adding an extra flag to re-implement this simple jq filter is not something we’re likely to do.

1 Like

I understand that more details might be valuable in different cases. I still need valid json output but not just the value itself.
So this `jq provides the solution:

jq 'with_entries(.value |= .value)'

Output

{"animal": "horse"}

it also handles different types.

3 Likes

I find this frustrating as I can have outputs that are entire modules. And I can get the output JSON formatted by doing

terraform output -json name_of_module_output

But there is no way to get the output of the main module in a similar format. It would be nice if we could specify something like:

terraform output -json .

To be explicit, I’m suggesting that using a placeholder such as “.” for name would refer to the root modules, but perform the same as the command when called with a NAME parameter.

For anyone else stumbling across this from Google, the below will generate the requested json:

terraform output -json | jq '[to_entries | .[] | {(.key): .value.value}] | add'