In the system where you wish to generate these JSONs, do you have access to the terraform binary itself, and are you able to include the terraform.io/builtin/terraform provider to your configuration? If yes, you could do something elegant, like this:
First add the terraform provider:
terraform {
required_providers {
terraform = {
source = "terraform.io/builtin/terraform"
}
}
}
Then use this to get your JSON, which you can then pipe to a file as required:
echo 'jsonencode(provider::terraform::decode_tfvars(file("${path.module}/PATH-TO-FILE.tfvars")))' | \
terraform console | \
jq -r | \
jq
The usage of jq is optional - it is used twice as the output of terraform console is a stringified JSON, so the first jq -r converts the string to a parseable JSON.
This makes use of the decode_tfvars function provided by terraform, which converts the contents of a .tfvars file to a terraform object.