Cách sắp xếp các khóa
Điểm chính
- Dán
- Chọn
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.
Các bước
- Paste the JSON into "Before".
- Tick "Sort keys".
- Press "Format".
- JSON with items in name order appears under "After".
- Sao chép bằng “Sao chép kết quả”.
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.
Phù hợp với
- Checking whether two JSONs have the same content
- Managing differences in configuration files
- Formatting samples in API specifications
Ví dụ
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".
Tóm tắt
Align key order before comparing. Array order doesn't change.
Dùng “Định dạng và kiểm tra JSON” (miễn phí)Công cụ hay dùng cùng
- Chuyển đổi CSV và JSON: Chuyển đổi giữa CSV và JSON ngay lập tức.
- Chuyển đổi tệp và Base64: Chuyển đổi giữa tệp và văn bản Base64.
- Xóa dòng trùng lặp và sắp xếp danh sách: Xóa các dòng trùng lặp và sắp xếp chúng.
Nếu hướng dẫn này hữu ích, một khoản ủng hộ nhỏ sẽ là niềm động viên lớn.
Gửi tiền ủng hộ (¥300)Tặng quà (danh sách mong muốn Amazon)