⚒️

pythonで出力したJSONっぽい文字列を整形して出力

2025/01/31に公開

忘備録として

前準備

jqのinstall

brew install jq

整形してみる

以下のような文字列があるとする

{'keyA': 180, 'keyB': {'str': 'aiueo', 'number': 12}}

jqコマンドを使って整形して出力

echo "<入力文>" | \
sed "s/'/\"/g" | jq .

入力例

echo "{'keyA': 180, 'keyB': {'str': 'aiueo', 'number': 12}}" | \
sed "s/'/\"/g" | jq .

結果

{
  "keyA": 180,
  "keyB": {
    "str": "aiueo",
    "number": 12
  }
}

Raycastなどsnippetを登録できるツールに登録しておくと便利

Discussion