Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert json to yaml and vice versa in command line?

How convert JSON file into YAML and vice versa in command line?

Any ways are welcome.

like image 352
rzlvmp Avatar asked Sep 13 '25 15:09

rzlvmp


1 Answers

Update: as of yq version 4.18:

The flags -p and -o let you specify input and output formats. You can convert between yaml, json, and xml!

yq -p json -o yaml file.json # json -> yaml
yq -p json -o xml file.json  # json -> xml
yq -p yaml -o json file.yaml # yaml -> json
yq -p yaml -o xml file.yaml  # yaml -> xml
yq -p xml -o json file.xml   # xml -> json
yq -p xml -o yaml file.xml   # xml -> yaml

With yq version 4.8.0:

yq e -P file.json yields YAML

yq e -j file.yaml yields JSON

  • e or eval evaluates each file separately. ea or eval-all merges them first.
  • -P or --prettyPrint outputs YAML
  • -j or --tojson outputs JSON
like image 130
pbatey Avatar answered Sep 15 '25 06:09

pbatey