Parser from terraform to json and vice versa

Hi,

We have requirement to edit .tf files and providing back to the user in one of our application. The language of our application is PHP but we can use CLI to parse files.

The approach we are taking to achieve this requirement is mentioned below:

  1. Take .tf files as input
  2. Parse .tf files in JSON so that application can easily read and edit JSON.
  3. Parse this JSON back to .tf files and provide to user

Now we are able to parse .tf files in JSON using this great library: GitHub - alecthomas/hcl: Parsing, encoding and decoding of HCL to and from Go types and an AST.
But unable to find a solution which can parse JSON to .tf files.

We tried to use below methods but is not upto the satisfaction.

For the sake of initial development, we are trying very basic outputs.tf file but our goal is to edit way more complex and full fledged terraform files.

outputs.tf:

output "virtual_machine_id" {
  value = azurerm_linux_virtual_machine.main.id
}

output "private_ip_address" {
  value = azurerm_network_interface.main.private_ip_address
}

outputs.json (from alecthomas/hcl with slight modification, removed ${} from expression):

{
    "output": {
        "private_ip_address": [
            {
                "value": "azurerm_network_interface.main.private_ip_address"
            }
        ],
        "virtual_machine_id": [
            {
                "value": "azurerm_linux_virtual_machine.main.id"
            }
        ]
    }
}

We need help to achieve this goal as we are new to GoLang and are unable find a satisfactory solution.

Thank you in advance.