HCL Conversion
Convert between HCL and JSON formats for interoperability and migration.
Use case: Integrating with tools that expect JSON, generating HCL from code, or migrating configurations.
HCL to JSON
Section titled “HCL to JSON”# Output to stdoutphp artisan huckle:hcl2json nodes.hcl
# Output to filephp artisan huckle:hcl2json nodes.hcl nodes.jsonuse Cline\Hcl\Hcl;
$hcl = <<<'HCL'name = "my-app"version = "1.0.0"enabled = trueports = [80, 443]HCL;
// Pretty-printed JSON (default)$json = Hcl::toJson($hcl);
// Compact JSON$json = Hcl::toJson($hcl, pretty: false);JSON to HCL
Section titled “JSON to HCL”# Output to stdoutphp artisan huckle:json2hcl nodes.json
# Output to filephp artisan huckle:json2hcl nodes.json nodes.hcluse Cline\Hcl\Hcl;
$json = '{"name": "my-app", "port": 8080}';
$hcl = Hcl::fromJson($json);// name = "my-app"// port = 8080Array to HCL
Section titled “Array to HCL”use Cline\Hcl\Hcl;
$data = [ 'name' => 'my-app', 'settings' => [ 'timeout' => 30, 'retries' => 3, ],];
$hcl = Hcl::arrayToHcl($data);Output:
name = "my-app"settings = { timeout = 30 retries = 3}Block Conversion
Section titled “Block Conversion”Nested structures are intelligently converted to HCL blocks:
$data = [ 'resource' => [ 'aws_instance' => [ 'web' => [ 'ami' => 'ami-12345', 'instance_type' => 't2.micro', ], ], ],];
$hcl = Hcl::arrayToHcl($data);Output:
resource "aws_instance" "web" { ami = "ami-12345" instance_type = "t2.micro"}Use Cases
Section titled “Use Cases”Export for Backup
Section titled “Export for Backup”# Export current config as JSON backupphp artisan huckle:hcl2json nodes.hcl backup-$(date +%Y%m%d).jsonGenerate from Code
Section titled “Generate from Code”// Build config programmatically$config = [ 'partition' => [ 'database' => [ 'environment' => [ 'production' => [ 'provider' => [ 'main' => [ 'host' => $productionHost, 'port' => 5432, ], ], ], ], ], ],];
$hcl = Hcl::arrayToHcl($config);file_put_contents('generated.hcl', $hcl);API Integration
Section titled “API Integration”// Parse HCL config$config = Hcl::parseFile('app.hcl');
// Send as JSON to API$response = Http::post('https://api.example.com/config', $config);