Skip to content

fixjson fails to fix JSON when the JSON file has duplicate keys #20

@w0rp

Description

@w0rp

This maybe an interesting problem to solve, because the JSON parser itself will be manifesting this error. I spotted this while editing my OpenCode configuration, in the following state:

{
  "$schema": "https://opencode.ai/config.json",
  "permission": {
    "bash": {
      "git *": "deny",
      "git diff *": "allow",
      "git show *": "allow",
      "python *": "deny",
      "python3 *": "deny",
      "pypy *": "deny",
      "ipython *": "deny",
      "uv *": "deny",
      "uv run pytest": "allow"
      "uv run pytest": "allow"
    },
    "read": {
      "*": "allow",
      "config.toml": "deny"
    }
  }
}
fixjson opencode.json 
Error: opencode.json: Cannot assign '"allow"' to the object's key 'uv run pytest'. Its value is already present as '"allow"' at line 15 column 0 of the JSON5 data. Still to read: "\n    },\n    \"read\": "

The prescence of the duplicate key stopped the file from being fixed. It would quite nice if fixjson could fix the syntax anyway and write back the formatted JSON with the duplicate key intact. Thenafter a linter for JSON can point out the duplicate key after commas have been inserted so that other linter's parser can read the JSON. My suggestion is to write a mapping for duplicate keys and re-map back to duplicated keys. Possibly hard to do!

Thank you so much for this project. I use it every day with ALE just about, and it's a life-saver!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions