1

I have a yaml file that looks something like this:

a:
  desc:
  value: 1
b:
  desc:
  value: 2
# ...

I want to convert it to this:

a: 1
b: 2
# ...

In yq v2, I used the command .[] |= .value' to update each element of the array to the value in the .value field. Is there a way to do this with yq v3?

cat config.yaml |  yq w - "*" "*.value"

yields

a: *.value
b: *.value
# ...
``
ethanabrooks
  • 747
  • 8
  • 19

1 Answers1

2

Your claim seems to be contradicting each other. There are two versions of yq implementations out there. A python implementation as a wrapper over jq and other written in Go.. See my answer that covers in details about those versions.

When you said you used .[] |= .value in yq v2, that's actually not the Go version, but the version with the Python wrapper over jq, since that syntax matches its DSL. But the other attempt yq w - "*" "*.value" seems to be the actual Go version.

Since there is an uncertainty around which version of yq in installed for you, I'll try to provide my view in both the versions

kislyuk's yq

yq -y '.[] |= .value' yaml

mikefarah's yq

The Go version does not have dynamic transformational capabilities like its Python version and does not support this type of update directly. Because the write/new field creations syntax is simply

yq w <yaml_file> <path_expression> <new value>

where the new value is not an expression but a literal value. Had it supported expressions, we could have conjured up a way to do the transformation. The Go version is otherwise good, but lacking support in some key transformational capabilities.

P.S. I've raised a GitHub feature request to allow such transformations. See https://github.com/mikefarah/yq/issues/602


As of today Dec 21st, 2020, yq v4 is in beta and supports this transformation. Download the v4 version and try

yq eval '.[] |= .value' test.yml
Inian
  • 80,270
  • 14
  • 142
  • 161