davydog187 / terraform-provider-jq

Terraform provider exposing the powerful jq JSON parser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Terraform Provider for jq

This plugin brings the power of jq to Terraform, allowing you to parse and extract complex data from JSON or even HCL objects.

Usage

No initialiation is required, you can begin using the provider immediately.

data "jq_query" "example" {
    data = "{\"a\": \"b\"}"
    query = ".a"
}

output "example" {
    value = data.jq_query.example.result
}

This will output:

Outputs:

example = "\"b\""

HCL Compatibility

The jq operates on json formatted strings. Fortunately, terraform provides the jsonencode() and jsondecode() functions for easily converting back and forth between HCL and json strings.

The above example in pure HCL:

data "jq_query" "example" {
    data = jsonencode({a = "b"})
    query = ".a"
}

output "example" {
    value = jsondecode(data.jq_query.example.result)
}

This will output:

Outputs:

example = "b"

Multiple Results

Some jq queries will produce multiple elements. In this case, the result will have multiple lines, with each line containing an element as a JSON encoded string. Keep in mind this means that the result cannot be converted to HCL with jsondecode() as the string itself is not valid JSON.

data "jq_query" "example" {
    data = jsonencode([{id:1},{id:2},{id:3}])
    query = ".[] | .id"
}

output "example" {
    value = data.jq_query.example.result
}

This will output:

Outputs:

example = <<EOT
1
2
3
EOT

Be sure to write your queries so they return a single element if you wish to convert them back to HCL with jsondecode().

About

Terraform provider exposing the powerful jq JSON parser

License:Apache License 2.0


Languages

Language:Go 64.2%Language:Makefile 20.8%Language:Dockerfile 15.0%