I'm interested in transforming from json format to tfvars using jq
, i.e.:
Input:
{
"foo": "aaa",
"bar": "bbb",
}
Desired output:
foo = "aaa"
bar = "bbb"
I tried
echo "{\"foo\": \"aaa\",\"bar\": \"bbb\"}" | jq '.[]'
"aaa"
"bbb"
I'm interested in transforming from json format to tfvars using jq
, i.e.:
Input:
{
"foo": "aaa",
"bar": "bbb",
}
Desired output:
foo = "aaa"
bar = "bbb"
I tried
echo "{\"foo\": \"aaa\",\"bar\": \"bbb\"}" | jq '.[]'
"aaa"
"bbb"
The tfvars specification is hard to come by, but numbers should not be quoted, null
is a special case, and arrays are also allowed as values, e.g. https://learn.hashicorp.com/tutorials/terraform/google-cloud-platform-variables?in=terraform/gcp-get-started gives the following as an example:
cidrs = [ "10.0.0.0/16", "10.1.0.0/16" ]
So the following should be closer to a general solution:
jq -r '
def q:
if type | IN("string", "boolean") then "\"\(tostring)\""
else .
end;
to_entries[] | "\(.key) = \(.value|q)"
'
A modification of the earlier answer might work.
jq -r 'to_entries[] | "\(.key) = \"\(.value)\""'