Skip to content

ys and yq

Today I met Mike Farah External link , the creator of yq External link !

I help maintain the go-yaml External link YAML framework for Go, and am working with Mike to help it fix certain issues in yq.

It looks like a promising future for both go-yaml and yq.

It turns out that YS has a lot of crossovers with yq.

Let's take a closer look at how they compare.

What is yq?🔗

yq is a command-line tool for querying and manipulating YAML (and other) data.

It is meant to work much like jq External link for JSON.

I've used yq a lot, and it's a great tool. It can do a few things that YS can't do. (At least, not yet!)

Quick Usage Guide🔗

Let's look at the examples in yq's Quick Usage Guide External link and compare them to YS.

Let's imagine we have the following file.yaml file:

a:
  b:
  - c: hello # A greeting

Read a value🔗

yq:

$ yq '.a.b[0].c' file.yaml
hello

YS:

$ ys '.a.b.0.c' file.yaml
hello

Pipe from STDIN🔗

yq:

$ yq '.a.b[0].c' < file.yaml
hello

YS:

$ ys '.a.b.0.c' < file.yaml
hello

Update a yaml file🔗

This is where yq really shines! It can update YAML and preserve the original formatting and comments!

yq:

$ yq '.a.b[0].c = "cool"' file.yaml
a:
  b:
    - c: cool # A greeting

Well, the - c: line got re-indented, but the comment is still there.

ys can't preserve comments but it can update the value. Not as nicely though.

YS:

$ ys -Ye '.assoc-in(["a" "b" 0 "c"] "cool")' file.yaml
a:
  b:
  - c: cool

Update using environment variables🔗

yq:

$ yq '.a.b[0].c = strenv(USER)' file.yaml
a:
  b:
    - c: ingy # A greeting

YS:

$ ys -Ye '.assoc-in(["a" "b" 0 "c"] ENV.USER)' file.yaml
a:
  b:
  - c: ingy

Merge multiple files🔗

Let's imagine we have the following file2.yaml file:

x:
  y:
    z: foobar # A comment

yq:

$ yq -n 'load("file.yaml") * load("file2.yaml")'
a:
  b:
    - c: hello # A greeting
x:
  y:
    z: foobar # A comment

YS:

$ ys -Ye 'load("file.yaml") + load("file2.yaml")'
a:
  b:
  - c: hello
x:
  y:
    z: foobar

Convert JSON to YAML🔗

Let's imagine we have the following file.json file:

{
  "a": "b",
  "c": "d"
}

yq:

$ yq -Poy file.json
a: b
c: d

YS:

$ ys -Y file.json
a: b
c: d

Conclusion🔗

I wish I had more time today to compare ys and yq more deeply.

You can see that at a certain level they both have a lot in common.

I'd encourage you to use both of them.

They are certainly complementary!

Comments