2

Is there a tool to flatten yaml structure like this:

foo: bar: baz: true 

into this:

foo.bar.baz = true 

not sure what is this syntax name

context: I need this for hcl, setting a lot of values like this:

set { name = "foo.bar.baz" value = false } 

this is for helm-provider in terraform, cannot use directly use json or yaml, have to set properties that you want to change one by one (i need to disable lots of stuff to check why it's not working).

6
  • Is the 2nd format intended to be a java properties file? You can use yamldecode to deserialize yaml. Commented Jul 6, 2023 at 18:26
  • oh no, need to create a lot of set { name = "foo.bar.baz", value = false } on hcl file XD Commented Jul 6, 2023 at 18:31
  • Do you mean this hcl? Can't you just convert your YAML to JSON and submit that? Commented Jul 6, 2023 at 18:40
  • 1
    Are you trying to convert a helm values file into a format for the terraform helm provider? If so, you can specify yaml as values = [] instead of set {}. See the doc here registry.terraform.io/providers/hashicorp/helm/latest/docs/… Commented Jul 6, 2023 at 20:42
  • 1
    Using values means you do not need to convert the yaml to a different format. Commented Jul 6, 2023 at 20:42

2 Answers 2

2

Using Andrey Kislyuk's yq to get all paths for the boolean values in the input document, for each path, join the path elements with dots, and then output each dotted path with the values found at that path.

yq -r ' paths(type == "boolean") as $p | [ ($p | join(".")), getpath($p) ] | join(" = ")' file 

Example input document:

foo: bar: baz: true baar: baz: false 

Resulting output:

foo.bar.baz = true foo.baar.baz = false 

To only get the paths ending with a key named enabled that has a true boolean value (as shown in an example elsewhere):

yq -r ' ( paths(type == "boolean" and .) | select(last == "enabled") ) as $p | [ ($p | join(".")), getpath($p) ] | join(" = ")' file 
0

Can't find anything, so I'll made it myself '__')

https://github.com/kokizzu/yamlflatten

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.