Prettier Plugin Sort JSON

raw JSON →
4.2.0 verified Sat Apr 25 auth: no javascript

A Prettier plugin that sorts JSON files alphanumerically by key. Version 4.2.0, actively maintained with regular releases. It supports custom sort orders, case-insensitive sorting, numeric sorting, and category-based sorting. Differentiators include deterministic key order for case-insensitive sorts, support for Prettier v3 (ESM), and TypeScript types. Requires Node.js >=18 and Prettier ^3.0.0.

error Cannot find module 'prettier-plugin-sort-json'
cause Prettier v3 no longer auto-loads plugins; plugin not in config
fix
Add plugins: ['prettier-plugin-sort-json'] to your Prettier config (e.g., .prettierrc.js)
error Error: jsonSortOrder must be a JSON string
cause Using a file path instead of JSON string for jsonSortOrder option
fix
Replace the file path with a JSON string like {"name": "alphabetical"}
error Error: Invalid sort order
cause jsonSortOrder contains invalid sort algorithm name or mismatched brackets
fix
Ensure jsonSortOrder is valid JSON and uses allowed sort options: alphabetically, natural, caseInsensitive, caseSensitive, none
error TypeError: prettier-plugin-sort-json is not a function
cause Incorrect import syntax in CJS project
fix
Use import or in CJS: const plugin = require('prettier-plugin-sort-json').default
breaking Drop support for Node.js v16 in v4.0.0
fix Upgrade to Node.js >=18.0.0
breaking Updated case insensitive sort to use deterministic key order in v4.0.0
fix Review JSON files that relied on original order for case-insensitive sorts; keys with same case may now be sorted differently
breaking Migration to Prettier v3 in v3.0.0 (plugin no longer auto-loaded)
fix Explicitly add plugin to prettier configuration (e.g., `plugins: ['prettier-plugin-sort-json']`)
breaking Changed `jsonSortOrder` option from file path to JSON string in v1.0.0
fix Update configuration to use JSON string instead of path (e.g., {"name": "alphabetical"})
deprecated Prettier v2 support removed in v3.0.0; use v2.x of this plugin for Prettier v2
fix Downgrade to prettier-plugin-sort-json@2 for Prettier v2, or upgrade to Prettier v3
gotcha jsonSortOrder validation error if not set (fixed in v4.1.1)
fix Set jsonSortOrder in prettier config or upgrade to v4.1.1+
npm install prettier-plugin-sort-json
yarn add prettier-plugin-sort-json
pnpm add prettier-plugin-sort-json

Shows how to configure Prettier with the plugin and a custom sort order, sorting 'name' and 'version' keys alphabetically and leaving others unsorted.

// .prettierrc.js
module.exports = {
  plugins: ['prettier-plugin-sort-json'],
  jsonSortOrder: '{"name": "alphabetical", "version": "alphabetical", "*": "none"}',
};

// Or use with CLI
// prettier --write "**/*.json" --plugin prettier-plugin-sort-json