If you work with with modern orchestration and configuration management systems you are most likely dealing with YAML and JSON on a daily basis. During testing, it is periodically useful to convert between these two formats, especially when interacting directly with API gateways. The yq Go program makes this incredibly easy and it has become a staple in my utility belt! To get started with yq you can snag it from github:
$ curl -L --output yq https://github.com/mikefarah/yq/releases/download/2.4.0/yq_linux_amd64
$ chmod 700 ./yq
Once downloaded you can review the available options by running yq without any arguments:
$ ./yq
Usage:
yq [flags]
yq [command]
Available Commands:
delete yq d [--inplace/-i] [--doc/-d index] sample.yaml a.b.c
help Help about any command
merge yq m [--inplace/-i] [--doc/-d index] [--overwrite/-x] [--append/-a] sample.yaml sample2.yaml
new yq n [--script/-s script_file] a.b.c newValue
prefix yq p [--inplace/-i] [--doc/-d index] sample.yaml a.b.c
read yq r [--doc/-d index] sample.yaml a.b.c
write yq w [--inplace/-i] [--script/-s script_file] [--doc/-d index] sample.yaml a.b.c newValue
Flags:
-h, --help help for yq
-t, --trim trim yaml output (default true)
-v, --verbose verbose mode
-V, --version Print version information and quit
Use "yq [command] --help" for more information about a command.
The yq project was started to provide a tool that worked like jq but operates on YAML instead of JSON. One feature I find myself using frequently is the read commands “–tojson” option:
$ ./yq r -j f.yml
{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"creationTimestamp":null,"labels":{"run":"nginx"},"name":"nginx"},"spec":{"replicas":1,"selector":{"matchLabels":{"run":"nginx"}},"strategy":{},"template":{"metadata":{"creationTimestamp":null,"labels":{"run":"nginx"}},"spec":{"containers":[{"image":"nginx","name":"nginx","resources":{}}]}}},"status":{}}
This will take a YAML file, process it and spit out a JSON object. Extremely handy for crafting the JSON objects you pass to the “–data” argument in a curl POST request:
$ curl --header "Content-Type: application/json" --request POST --data <yq output goes here> http://api/path/to/rest/api
In a follow up post I will show how you can use this amazing utility to slice and dice yaml. Amazing tool!