如何在命令行中将 json 转换为 yaml,反之亦然?

rzl*_*vmp 12 linux command-line json yaml converters

如何在命令行中将 JSON 文件转换为 YAML,反之亦然?

欢迎任何方式。

pba*_*tey 34

更新:从yq版本 4.18开始:

标志-p-o让您指定输入和输出格式。您可以在 yaml、jsonxml 之间进行转换!

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
Run Code Online (Sandbox Code Playgroud)

对于yq版本 4.8.0:

yq e -P file.json产生 YAML

yq e -j file.yaml产生 JSON

  • eeval单独评估每个文件。ea或者eval-all先合并它们。
  • -P--prettyPrint输出 YAML
  • -j--tojson输出 JSON