Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FQ fails to parse YAML files containing multiple documents #1087

Open
visualphoenix opened this issue Feb 27, 2025 · 4 comments
Open

FQ fails to parse YAML files containing multiple documents #1087

visualphoenix opened this issue Feb 27, 2025 · 4 comments

Comments

@visualphoenix
Copy link

visualphoenix commented Feb 27, 2025

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
wader added a commit that referenced this issue Feb 27, 2025
Related to #1087

TODO:
Support encode via option?
@wader
Copy link
Owner

wader commented Feb 27, 2025

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?

@visualphoenix
Copy link
Author

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
Copy link
Author

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.

wader added a commit that referenced this issue Feb 28, 2025
@wader
Copy link
Owner

wader commented Feb 28, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants