18

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?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
fisdaf
  • 183
  • 1
  • 1
  • 4

1 Answers1

27

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"' 
Diaa Sami
  • 3,249
  • 24
  • 31
Inian
  • 80,270
  • 14
  • 142
  • 161
  • Hey, that's exactly what I was looking for, thanks, for some reason I thought it would be something more similar to jsonpath – fisdaf Nov 24 '20 at 11:24
  • 2
    Starting 4.18.1, the `e` is not anymore needed as it is the default and `-` can also be dropped if reading from stdin: https://mikefarah.gitbook.io/yq/#notice-for-v4.x-versions-prior-to-4.18.1. We can simplify the command to `yq '.a."b.c"'` – Gino Mempin Jul 20 '22 at 03:47
  • FYI, in Helm v3.7.2 something like `cat values.yaml | yq '.field."field.with.dots"'` works. – ryuzakyl Dec 09 '22 at 08:42