richie5um / vscode-sort-json

VSCode Extension to Sort JSON objects
https://marketplace.visualstudio.com/items?itemName=richie5um2.vscode-sort-json
MIT License
107 stars 20 forks source link

VSCode SortJSON

What is it?

Alphabetically sorts the keys in selected JSON objects.

Please take care using this - I've tried to ensure it won't invalidate your JSON. But, as it has to parse > sort > stringify, there is a chance it'll lose something. It should be fine for plain JSON.

Buy me a coffee

Install

Usage

Context Menu

If there are too many entries in the Context Menu, then you can modify which get shown by adding (and editing) this to your VSCode preferences:

"sortJSON.contextMenu": {
    "sortJSON": false,
    "sortJSONAlphaNum": false,
    "sortJSONAlphaNumReverse": false,
    "sortJSONKeyLength": false,
    "sortJSONKeyLengthReverse": false,
    "sortJSONReverse": false,
    "sortJSONType": false,
    "sortJSONTypeReverse": false,
    "sortJSONValues": false,
    "sortJSONValuesReverse": false
}

Updates

Example

Example

Settings

Sort on save

There's a vscode setting for formatters (settings.json):

  "editor.codeActionsOnSave": {
    "source.fixAll": true
  }

But you can also selectively enable/disable this formatter with (settings.json):

{
    "editor.codeActionsOnSave": {
        "source.fixAll.sortJSON": true
    }
}

Or use a hotkey, if you prefer (keybindings.json):

{
    "key": "cmd+shift+a",
    "command": "editor.action.codeAction",
    "args": {
        "kind": "source.fixAll.sortJSON"
    }
}