https://github.com/tillig/vscode-json-stable-stringify
VSCode extension to format JSON using json-stable-stringify
https://github.com/tillig/vscode-json-stable-stringify
formatter json vscode vscode-extension
Last synced: about 2 months ago
JSON representation
VSCode extension to format JSON using json-stable-stringify
- Host: GitHub
- URL: https://github.com/tillig/vscode-json-stable-stringify
- Owner: tillig
- License: other
- Created: 2019-03-15T20:57:51.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-07-18T21:18:15.000Z (almost 3 years ago)
- Last Synced: 2025-02-01T12:27:54.431Z (over 1 year ago)
- Topics: formatter, json, vscode, vscode-extension
- Language: TypeScript
- Homepage: https://marketplace.visualstudio.com/items?itemName=TravisIllig.vscode-json-stable-stringify
- Size: 433 KB
- Stars: 2
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Sort JSON (Stable)
The `vscode-json-stable-stringify` extension is used to sort a JSON object in VS Code using the [`json-stable-stringify` sort mechanism](https://github.com/ljharb/json-stable-stringify) to create a standardized, sorted JSON object.
While it will use the editor's current settings for indentation, it otherwise uses default settings for the sort.
## Usage
This extension implements only a command accessible via the command palette (`Ctrl+Shift+P`) called `Sort JSON (Stable)`. If you have one or more selections in the editor, the selections will be parsed as JSON, sorted, and formatted. If you have no selection, the whole document will be sorted and formatted.
**Why use this instead of some other formatter/sorter?**
Some JSON sorters implement a document formatter for JSON so the `Format Document` command automatically sorts. Unfortunately, if you have _multiple_ document formatters for a given document type [only one will run and it's not configurable/clear which](https://github.com/Microsoft/vscode/issues/41882). By separating the option to sort out of a document formatter, you can use other formatters like Prettier or Beautify to handle things before/after the sort as a separate concern.
Some JSON sorters have too many options or try to do too much and can the results can be unpredictable/unreliable. This one goes for simple and standard using a tried and tested library that has been around for a while [(`json-stable-stringify`)](https://github.com/ljharb/json-stable-stringify).
Finally, some JSON sorters are hard-tied to the JSON document type. This ignores JSON that might be embedded in a JavaScript file, or even JSON that appears in an HTML file as part of an embedded script. There's no language tie here - highlight something JSON and sort it. No judgment.
## You'll Lose Comments
This extension can parse JSON with comments via the JSON5 parser, but **when writing the sorted JSON back out your comments will disappear** (as will other JSON extensions like trailing commas).
## CLI and Pre-Commit Hook
There's [an associated CLI tool and pre-commit hook](https://github.com/tillig/json-sort-cli) you can use to integrate sorting into your build.