A Prettier plugin that removes duplicate keys in JSON files and maintains proper indentation. When duplicate keys are found, the plugin keeps the last occurrence of the key-value pair.
- Removes duplicate keys in JSON files
- Keeps the last occurrence of duplicate keys
- Maintains proper indentation
- Sorts keys alphabetically
- Works recursively on nested objects
- Preserves arrays and their contents
npm install --save-dev prettier-plugin-remove-duplicate-json-keys
or
yarn add --dev prettier-plugin-remove-duplicate-json-keys
The plugin will be automatically picked up by Prettier. Just run Prettier as usual:
prettier --write "**/*.json"
Input:
{
"name": "John",
"age": 30,
"name": "Jane",
"nested": {
"a": 1,
"b": 2,
"a": 3
}
}
Output:
{
"age": 30,
"name": "Jane",
"nested": {
"a": 3,
"b": 2
}
}
The plugin supports the following options:
Type: boolean
Default: true
Controls whether to sort JSON object keys lexically. When enabled, all object keys will be sorted in alphabetical order. When disabled, the original order of keys will be preserved (except for duplicate keys, where the last occurrence is kept).
Example configuration in .prettierrc
:
{
"enableSortJSON": false
}
Or via CLI:
prettier --write --enable-sort-json=false "**/*.json"
The plugin also respects Prettier's built-in options like tabWidth
.
MIT