Search code examples
jsonintellij-ideaformattingwhitespaceempty-list

How to format spaces in IDEA JSON formatting for empty objects and arrays


I have large JSON file with a lot of empty objects and arrays, but when i format this file in Intellij Idea it adds automatically spaces into all empty objects and arrays.

So, was:

{
  "someObject": {},
  "someArray": []
}

became:

{
  "someObject": { },
  "someArray": [ ]
}

and all other lines like this.

Since our team uses different editors/formatters for this file, i can't commit such changes, because this will be "formatting war" with other people on the same project, who use other editors for JSON.

In IDEA Settings->Editor->Code Style->JSON i found these settings: IDEA JSON formatting settings

but looks like nothing about "empty object/array spaces".

So, the question is: is it possible to change default empty object/array formatting in JSON for Intellij IDEA? May be i can patch some formatter plugin or install other one for JSON?


Solution

  • As per the comment above, your settings look fine and I can confirm that on my local 14.1.7 installation they work as expected.

    but looks like nothing about "empty object/array spaces"

    The settings which you are showing, Editor -> Code Style -> JSON -> Spaces tab are what your looking for:

    • The Braces checkbox handles objects
    • The Brackets checkbox handles arrays

    You can add an empty object/array in the preview section on the right, and try to check/uncheck again those 2 checkboxes to see what happens:

    JSON Formatter settings