JSONを1行にまとめる方法
- 貼る
- まとめる
- コピーする
読みやすく整えたJSONを、今度は、通信量を減らしたり、1行で扱ったりするために、逆に詰めたいことがあります。JSONを整える・チェックする の「1行にまとめる」で、空白と改行を取り除けます。
手順
- 「整える前」に、JSONを貼り付けます。
- 「1行にまとめる」を押します。
- 「整えた後」に、1行になったJSONが出ます。
- 「結果をコピー」でコピーして使います。
1行にまとめるメリット
- ファイルや通信の容量が、小さくなる
- 1行のデータとして、ほかのシステムに渡せる
- ログや、環境変数に、入れやすい
- コマンドラインで、引数として渡しやすい
整える・まとめる、の使い分け
- 整える:人が読むとき、編集するとき
- 1行にまとめる:機械に渡すとき、保存するとき
注意すること
- 1行にしたJSONは、人が読むのが大変です。元の整えたJSONも、残しておくと便利です。
- 文字列の中の空白や改行は、そのまま残ります。
容量が減る目安
空白と改行が多いほど、効果が大きくなります。字下げの深いJSONでは、2〜3割小さくなることもあります。
向いている場面
- APIのリクエストの、本文を作る
- 設定を、環境変数に入れる
- ログに、JSONを1行で出力する
- データベースの項目に、保存する
例
読みやすく整えた設定のJSONを、環境変数に入れたかった。貼り付けて「1行にまとめる」を押し、結果をコピーして設定した。
つまずきやすい点
- エラーになる:JSONの書き方に誤りがある。整える前に、チェックする。
- 日本語の文字が変わる:このツールは、日本語をそのまま残す。
- 元に戻したい:「整える」を押せば、また字下げされる。
よくある質問
1行にしたJSONを、読みやすく戻せますか?
はい。「整える」を押すと、字下げされます。
まとめ
機械に渡すなら1行に、人が読むなら整える。元のJSONも、残しておきましょう。
JSONを整える・チェックするを使う(無料)あわせて使えるツール
- CSVとJSONを相互変換する:CSVとJSONを、すぐに相互変換します。
- ファイルとBase64を相互変換する:ファイルとBase64の文字を相互に変換します。
- 重複した行を消す・並べ替える:重複した行を消して、並べ替えます。
同じツールの、ほかの解説
How to Minify JSON
- Paste
- Minify
- Copy
JSON you've formatted for reading sometimes needs to be packed back, to cut data volume or handle it on one line. "Make one line" in Format and Validate JSON removes spaces and line breaks.
Steps
- Paste the JSON into "Before".
- Press "Make one line".
- The one-line JSON appears under "After".
- Copy with "Copy result" and use it.
Benefits of one line
- File and communication sizes get smaller
- It can be passed to other systems as one line of data
- Easy to put in logs and environment variables
- Easy to pass as an argument on the command line
When to format and when to compress
- Format: when people read or edit it
- Make one line: when handing to machines or storing
Cautions
- One-line JSON is hard for people to read. Keeping the formatted original is handy.
- Spaces and line breaks inside strings remain as they are.
How much smaller
The more spaces and line breaks, the bigger the effect. Deeply indented JSON can shrink by 20–30%.
Good for
- Building the body of an API request
- Putting settings into an environment variable
- Outputting JSON on one line in logs
- Saving to a database field
Example
Wanted to put formatted configuration JSON into an environment variable. Pasted it, pressed "Make one line", then copied the result and set it.
Common stumbling blocks
- An error appears: there is a mistake in the JSON syntax. Check before formatting.
- Japanese characters change: this tool leaves Japanese as it is.
- Want to go back: pressing "Format" indents it again.
Frequently asked questions
Can one-line JSON be turned back into readable form?
Yes. Pressing "Format" indents it.
Summary
One line for machines, formatted for people. Keep the original JSON too.
Use "Format and Validate JSON" (free)Tools that go well with this
- Convert CSV and JSON: Convert between CSV and JSON instantly.
- Convert Files and Base64: Convert between files and Base64 text.
- Remove Duplicate Lines and Sort a List: Remove duplicate lines and sort them.