キーの順番をそろえる方法
この記事のポイント
- 貼る
- 印をつける
2つのJSONを比べたいのに、項目の並び順が違うだけで、差分が大量に出てしまう。そんなときは、JSONを整える・チェックする の「キーの順番を並べ替える」で、順番をそろえてから比べます。
手順
- 「整える前」に、JSONを貼り付けます。
- 「キーの順番を並べ替える」にチェックを入れます。
- 「整える」を押します。
- 「整えた後」に、項目名の順に並んだJSONが出ます。
- 「結果をコピー」でコピーします。
並べ替えは、項目名の順(辞書順)です。入れ子の中の項目も、並べ替えられます。
並べ替えの効果
- 同じ内容のJSONが、同じ並びになる
- 差分の比較が、しやすくなる
- 項目を探しやすくなる
- バージョン管理で、無駄な差分が減る
比較の手順
- 比べたい2つのJSONを、それぞれ「キーの順番を並べ替える」で整える。
- 整った結果を、2つの文章の差分を調べるツールに貼り付ける。
- 本当の違いだけが、表示される。
並べ替えても変わらないこと
- 項目と値の対応は、変わりません。
- 配列(リスト)の中の順序は、変わりません。配列は、並び順に意味があるためです。
注意すること
- 項目の並び順に意味があるデータ(設定の読み込み順など)は、並べ替えると、意味が変わる場合があります。
- 元の並び順を残したいときは、チェックを入れないで整えます。
向いている場面
- 2つのJSONの、内容が同じかの確認
- 設定ファイルの、差分の管理
- API仕様書のサンプルの、整形
例
2つのシステムが出力したJSONを比べたかった。項目の並びが違うため、見た目では分からなかった。両方を並べ替えてから比べたところ、実際に違う項目は1つだけだった。
つまずきやすい点
- 並べ替えたら、読みにくくなった:意味のある並びだった。チェックを外す。
- 配列の中身は並ばない:配列の順序は、保たれる。
よくある質問
数字の項目名は、どう並びますか?
文字として並びます。「10」は「2」より前になります。
まとめ
比べる前に、キーの順番をそろえる。配列の順序は、変わりません。
JSONを整える・チェックするを使う(無料)あわせて使えるツール
- CSVとJSONを相互変換する:CSVとJSONを、すぐに相互変換します。
- ファイルとBase64を相互変換する:ファイルとBase64の文字を相互に変換します。
- 重複した行を消す・並べ替える:重複した行を消して、並べ替えます。
同じツールの、ほかの解説
How to Sort the Keys
Key points
- Paste
- Tick
You want to compare two JSONs, but a mass of differences appears just because item order differs. In that case, align the order first with "Sort keys" in Format and Validate JSON and then compare.
Steps
- Paste the JSON into "Before".
- Tick "Sort keys".
- Press "Format".
- JSON with items in name order appears under "After".
- Copy it with "Copy result".
Sorting is by item name (dictionary order). Items inside nesting are sorted too.
Effects of sorting
- JSON with the same content gets the same order
- Comparing differences becomes easier
- Items become easier to find
- Pointless differences in version control decrease
Steps for comparing
- Format each of the two JSONs with "Sort keys".
- Paste the results into the tool that finds differences between two texts.
- Only the real differences are shown.
What does not change with sorting
- The pairing of items and values doesn't change.
- The order inside arrays (lists) doesn't change, because order in arrays is meaningful.
Cautions
- For data where item order matters (such as the loading order of settings), sorting can change the meaning.
- When you want to keep the original order, format without ticking.
Good for
- Checking whether two JSONs have the same content
- Managing differences in configuration files
- Formatting samples in API specifications
Example
Wanted to compare JSON output by two systems. Item order differed, so it was unclear by eye. After sorting both and comparing, only one item actually differed.
Common stumbling blocks
- It got harder to read after sorting: the order was meaningful. Untick.
- Array contents are not sorted: array order is preserved.
Frequently asked questions
How are numeric item names ordered?
As characters. "10" comes before "2".
Summary
Align key order before comparing. Array order doesn't change.
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.