|
| 1 | +--- |
| 2 | +title: ys and yq |
| 3 | +date: 2025-07-22 |
| 4 | +draft: false |
| 5 | +authors: [ingydotnet] |
| 6 | +categories: [Summer-of-YS] |
| 7 | +edit: blog/2025-07-22.md |
| 8 | +comments: true |
| 9 | +--- |
| 10 | + |
| 11 | +Today I met [Mike Farah](https://github.com/mikefarah), the creator of |
| 12 | +[yq](https://github.com/mikefarah/yq)! |
| 13 | + |
| 14 | +I help maintain the [go-yaml](https://github.com/yaml/go-yaml) YAML framework |
| 15 | +for Go, and am working with Mike to help it fix certain issues in yq. |
| 16 | + |
| 17 | +It looks like a promising future for both go-yaml and yq. |
| 18 | + |
| 19 | +It turns out that YS has a lot of crossovers with yq. |
| 20 | + |
| 21 | +Let's take a closer look at how they compare. |
| 22 | + |
| 23 | +<!-- more --> |
| 24 | + |
| 25 | + |
| 26 | +## What is yq? |
| 27 | + |
| 28 | +yq is a command-line tool for querying and manipulating YAML (and other) data. |
| 29 | + |
| 30 | +It is meant to work much like [jq](https://stedolan.github.io/jq/) for JSON. |
| 31 | + |
| 32 | +I've used yq a lot, and it's a great tool. |
| 33 | +It can do a few things that YS can't do. |
| 34 | +(At least, not yet!) |
| 35 | + |
| 36 | + |
| 37 | +## Quick Usage Guide |
| 38 | + |
| 39 | +Let's look at the examples in [yq's Quick Usage Guide]( |
| 40 | +https://github.com/mikefarah/yq?tab=readme-ov-file#quick-usage-guide) and |
| 41 | +compare them to YS. |
| 42 | + |
| 43 | +Let's imagine we have the following `file.yaml` file: |
| 44 | + |
| 45 | +```yaml |
| 46 | +a: |
| 47 | + b: |
| 48 | + - c: hello # A greeting |
| 49 | +``` |
| 50 | +
|
| 51 | +### Read a value |
| 52 | +
|
| 53 | +yq: |
| 54 | +
|
| 55 | +```bash |
| 56 | +$ yq '.a.b[0].c' file.yaml |
| 57 | +hello |
| 58 | +``` |
| 59 | + |
| 60 | +YS: |
| 61 | + |
| 62 | +```bash |
| 63 | +$ ys '.a.b.0.c' file.yaml |
| 64 | +hello |
| 65 | +``` |
| 66 | + |
| 67 | + |
| 68 | +### Pipe from STDIN |
| 69 | + |
| 70 | +yq: |
| 71 | + |
| 72 | +```bash |
| 73 | +$ yq '.a.b[0].c' < file.yaml |
| 74 | +hello |
| 75 | +``` |
| 76 | + |
| 77 | +YS: |
| 78 | + |
| 79 | +```bash |
| 80 | +$ ys '.a.b.0.c' < file.yaml |
| 81 | +hello |
| 82 | +``` |
| 83 | + |
| 84 | + |
| 85 | +## Update a yaml file |
| 86 | + |
| 87 | +This is where yq really shines! |
| 88 | +It can update YAML and preserve the original formatting and comments! |
| 89 | + |
| 90 | +yq: |
| 91 | + |
| 92 | +```bash |
| 93 | +$ yq '.a.b[0].c = "cool"' file.yaml |
| 94 | +a: |
| 95 | + b: |
| 96 | + - c: cool # A greeting |
| 97 | +``` |
| 98 | + |
| 99 | +Well, the `- c:` line got re-indented, but the comment is still there. |
| 100 | + |
| 101 | +ys can't preserve comments but it can update the value. |
| 102 | +Not as nicely though. |
| 103 | + |
| 104 | +YS: |
| 105 | + |
| 106 | +```bash |
| 107 | +$ ys -Ye '.assoc-in(["a" "b" 0 "c"] "cool")' file.yaml |
| 108 | +a: |
| 109 | + b: |
| 110 | + - c: cool |
| 111 | +``` |
| 112 | + |
| 113 | + |
| 114 | +### Update using environment variables |
| 115 | + |
| 116 | +yq: |
| 117 | + |
| 118 | +```bash |
| 119 | +$ yq '.a.b[0].c = strenv(USER)' file.yaml |
| 120 | +a: |
| 121 | + b: |
| 122 | + - c: ingy # A greeting |
| 123 | +``` |
| 124 | + |
| 125 | +YS: |
| 126 | + |
| 127 | +```bash |
| 128 | +$ ys -Ye '.assoc-in(["a" "b" 0 "c"] ENV.USER)' file.yaml |
| 129 | +a: |
| 130 | + b: |
| 131 | + - c: ingy |
| 132 | +``` |
| 133 | + |
| 134 | + |
| 135 | +### Merge multiple files |
| 136 | + |
| 137 | +Let's imagine we have the following `file2.yaml` file: |
| 138 | + |
| 139 | +```yaml |
| 140 | +x: |
| 141 | + y: |
| 142 | + z: foobar # A comment |
| 143 | +``` |
| 144 | +
|
| 145 | +yq: |
| 146 | +
|
| 147 | +```bash |
| 148 | +$ yq -n 'load("file.yaml") * load("file2.yaml")' |
| 149 | +a: |
| 150 | + b: |
| 151 | + - c: hello # A greeting |
| 152 | +x: |
| 153 | + y: |
| 154 | + z: foobar # A comment |
| 155 | +``` |
| 156 | +
|
| 157 | +YS: |
| 158 | +
|
| 159 | +```bash |
| 160 | +$ ys -Ye 'load("file.yaml") + load("file2.yaml")' |
| 161 | +a: |
| 162 | + b: |
| 163 | + - c: hello |
| 164 | +x: |
| 165 | + y: |
| 166 | + z: foobar |
| 167 | + |
| 168 | +``` |
| 169 | + |
| 170 | + |
| 171 | +### Convert JSON to YAML |
| 172 | + |
| 173 | +Let's imagine we have the following `file.json` file: |
| 174 | + |
| 175 | +```json |
| 176 | +{ |
| 177 | + "a": "b", |
| 178 | + "c": "d" |
| 179 | +} |
| 180 | +``` |
| 181 | + |
| 182 | + |
| 183 | +yq: |
| 184 | + |
| 185 | +```bash |
| 186 | +$ yq -Poy file.json |
| 187 | +a: b |
| 188 | +c: d |
| 189 | +``` |
| 190 | + |
| 191 | +YS: |
| 192 | + |
| 193 | +```bash |
| 194 | +$ ys -Y file.json |
| 195 | +a: b |
| 196 | +c: d |
| 197 | +``` |
| 198 | + |
| 199 | + |
| 200 | +## Conclusion |
| 201 | + |
| 202 | +I wish I had more time today to compare ys and yq more deeply. |
| 203 | + |
| 204 | +You can see that at a certain level they both have a lot in common. |
| 205 | + |
| 206 | +I'd encourage you to use both of them. |
| 207 | + |
| 208 | +They are certainly complementary! |
0 commit comments