Search code examples
jsonlinuxcommand-lineyamlconverters

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.


Solution

  • 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