OpenToFu: Prettifying the state file output

When we run tofu plan or tofu apply with the -state option, it creates a state file containing resource details, similar to Terraform. However, in OpenTofu, the JSON state file is not saved in a pretty-printed format instead, it is stored as a single-line JSON string.

If a formatted JSON structure for the state file is required, there are two ways to achieve it:

  • By using -json option with output:
tofu output -json -state=<path_to_state_file>

This option outputs the state file content in a prettified JSON format, but it omits certain properties, such as "version", from the output.

NOTE: tofu state show also has a -json option which behaves similar to output -json.

  • By using the bash “jq” command:
cat <path_to_state_file> | jq

This option outputs the statefile content in a prettified JSON format without omitting any property from the output.