prettier-plugin-remove-duplicate-json-keys

1.2.0 • Public • Published

prettier-plugin-remove-duplicate-json-keys

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.

Features

  • 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

Installation

npm install --save-dev prettier-plugin-remove-duplicate-json-keys

or

yarn add --dev prettier-plugin-remove-duplicate-json-keys

Usage

The plugin will be automatically picked up by Prettier. Just run Prettier as usual:

prettier --write "**/*.json"

Example

Input:

{
  "name": "John",
  "age": 30,
  "name": "Jane",
  "nested": {
    "a": 1,
    "b": 2,
    "a": 3
  }
}

Output:

{
  "age": 30,
  "name": "Jane",
  "nested": {
    "a": 3,
    "b": 2
  }
}

Configuration

The plugin supports the following options:

enableSortJSON

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.

License

MIT

Package Sidebar

Install

npm i prettier-plugin-remove-duplicate-json-keys

Weekly Downloads

2

Version

1.2.0

License

MIT

Unpacked Size

8.83 kB

Total Files

4

Last publish

Collaborators

  • dearestzz