シェルスクリプト内で とある YAML ファイル(あるいはデータ)を JSON で出力したい時 例えば「 sample.yaml 」ファイルを JSON 形式に変換したい場合は、下記のようにするとできます。 ruby -rjson -ryaml -e 'print YAML.load(STDIN.read).to_json' < sample.yaml # 出力 : JSON もちろんパイプで入力してもいいですし、結果を jq コマンドなどに喰わせてもいいです。 cat sample.yaml | \ ruby -rjson -ryaml -e 'print YAML.load(STDIN.read).to_json' | \ jq . シェルスクリプト内なら、関数にしてしまうと使い勝手がいいですね。 function yaml_to_json(){ ruby -rjson -ryaml
![[小ネタ] シェルスクリプト内で YAML -> JSON 変換する #ruby | DevelopersIO](https://cdn-ak-scissors.b.st-hatena.com/image/square/499289c4ea21cd46a0311d1581b6e4728e5d304c/height=288;version=1;width=512/https%3A%2F%2Fdevio2023-media.developers.io%2Fwp-content%2Fuploads%2F2019%2F04%2Fruby-eyecatch-1200x630.png)