LoginSignup
1
0

More than 1 year has passed since last update.

【Shell】エスケープ文字を含むJSONを出力する

Last updated at Posted at 2022-12-24

やりたいこと

シェルスクリプトで以下のようなエスケープ文字を含むJsonファイルを出力したい

{
    "hoge": "fuga",
    "nested_json": "{ \"hoge\": \"fuga\"}"
}

ネストしたjsonの中でエスケープ文字を使うときなど、上記を実現するためには\"といったエスケープ処理自体をエスケープして出力する必要がある。

解決策

\\\"を使う。


json="{\"hoge\": \"fuga\", \"nested_json\": \"{\\\"hoge\\\": \\\"fuga\\\"}\"}"

echo $json > result.json

結果

{"hoge": "fuga", "nested_json": "{\"hoge\": \"fuga\"}"}

スクリーンショット 2022-11-16 17.15.01.png

syntaxエラーが出ずに無事出力できました!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0