How to read values with yq when the key has a dot?
Asked Answered
S

1

21

I am having issues reading values from a YAML file when there's a dot in the key name.

i.e.

a:
 b.c: 2

Reading the a key works fine with cat mytext.yaml | yq r - a. However, when I tried reading a.b.c it doesn't give any output.

I tried escaping the dot symbol but that doesn't give any output.

Anything I am missing here?

Scotia answered 24/11, 2020 at 8:49 Comment(0)
F
33

On v4 onwards, you could simply use the new syntax notation, i.e.

echo 'a:
 b.c: 2' | yq e '.a."b.c"' - 

In mikefarah/yq, you can use the quotes "..", to preserve the field containing . in your path expression as explained in the documentation Nested special characters

echo 'a:
 b.c: 2' | yq r - 'a."b.c"' 
Fruit answered 24/11, 2020 at 10:44 Comment(3)
Hey, that's exactly what I was looking for, thanks, for some reason I thought it would be something more similar to jsonpathScotia
Starting 4.18.1, the e is not anymore needed as it is the default and - can also be dropped if reading from stdin: mikefarah.gitbook.io/yq/…. We can simplify the command to yq '.a."b.c"'Sublunary
FYI, in Helm v3.7.2 something like cat values.yaml | yq '.field."field.with.dots"' works.Harlanharland

© 2022 - 2024 — McMap. All rights reserved.