키를 정렬하는 방법
핵심 포인트
- 붙여넣기
- 체크
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.
순서
- Paste the JSON into "Before".
- Tick "Sort keys".
- Press "Format".
- JSON with items in name order appears under "After".
- “결과 복사”로 복사합니다.
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.
알맞은 경우
- Checking whether two JSONs have the same content
- Managing differences in configuration files
- Formatting samples in API specifications
예
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".
요약
Align key order before comparing. Array order doesn't change.
“JSON 정리 및 검증” 사용하기 (무료)함께 쓰면 좋은 도구
- CSV와 JSON 변환: CSV와 JSON을 즉시 서로 변환합니다.
- 파일과 Base64 변환: 파일과 Base64 텍스트를 서로 변환합니다.
- 중복된 줄 제거 및 목록 정렬: 중복된 줄을 제거하고 정렬합니다.