fq icon indicating copy to clipboard operation
fq copied to clipboard

FQ fails to parse YAML files containing multiple documents

Open visualphoenix opened this issue 10 months ago • 4 comments

What version are you using (fq -v)?

$ fq -v
0.13.0 (darwin arm64 go1.23.5)

How was fq installed?

FQ was installed with Nix on macOS.

Can you reproduce the problem using the latest release or master branch?

I have verified that this issue occurs in version 0.13.0. I haven't checked the master branch, but I can try if needed.

What did you do?

I attempted to parse a YAML file containing multiple documents using fq.

To demonstrate:

$ cat b
---
hi: bye
---
doesnt: work

$ cat b | fq .

What result did you expect?

I expected fq to parse the YAML file and output each document separately as a stream or in an array format, similar to how it handles a single document.

What did you see instead?

error: <stdin>: probe: failed to decode: try fq -d FORMAT to force format, see fq -h formats for list

visualphoenix avatar Feb 27 '25 17:02 visualphoenix

Hi, thanks for the report! That should be possible to fix but i'm a bit unsure how it should work. I did a draft PR #1088 that decodes all documents into an array but then these two yaml files will produce the same jq value:

---
hi: bye
---
doesnt: work
- hi: bye
- doesnt: work

Maybe not a big deal. But maybe the yaml format,to_yaml and from_yaml should have an options to force multi document mode?

wader avatar Feb 27 '25 22:02 wader

In my case I’m dealing with kubernetes manifests and helm template output - eg: https://github.com/kubernetes-sigs/gateway-api/releases/download/v1.2.1/experimental-install.yaml

It’s useful to do analysis on kinds. Right now I have to use yq to parse the multi document yaml into json and then use fq.

Eg: cat foo.yaml | yq e -o json | fq .

Would be nice to get rid of yq entirely. That’s my last missing use case

visualphoenix avatar Feb 27 '25 23:02 visualphoenix

It would also be nice if it preserved ordering eg

cat experimental-install.yaml | fq -rc to_yaml

Was a multi document output ideally with stable key ordering. I know that might be tough with go.

visualphoenix avatar Feb 28 '25 00:02 visualphoenix

Now the draft PR has has a multi_document options for both decode and encode to force multi document mode.

If you have go installed you should be albe to try it out using:

$ go run github.com/wader/fq@yaml-multi-document -nr '[1,2,3] | to_yaml({multi_document: true})'
1
---
2
---
3

Was a multi document output ideally with stable key ordering. I know that might be tough with go.

Yes preserving order in object is a bit tricky as it works now. Another thing that won't work is comments in yaml, they will be stripped. It's a bit unclear how that would work.

wader avatar Feb 28 '25 10:02 wader