question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow JSON with trailing commas

See original GitHub issue

I’m looking for a way to support the following style:

  • JSON-based
  • Always quote keys
  • Always include trailing commas

This would typically be not a problem – I should be able to set parser: json and trailingComma: all. However, #2308* disables this specific combination and I can’t get around it.

Here’s my use case: I want Prettier to format my VSCode settings files, which I keep in a git repo.

  • VSCode requires keys to be quoted, so I can’t use the json5 parser.
  • I would like trailing commas to make diffs more readable.

Note: I originally filed this bug in another repo https://github.com/prettier/prettier-vscode/issues/589

* I am aware that #2308 was created because trailing commas broke VSCode. However, times have changed and VSCode settings files now support trailing commas.

Thanks!

Prettier 1.15.3 Playground link

--parser json
--trailing-comma all

Input:

{
  //
  "a": 1,
  "b": 2,
}

Output:

{
  //
  "a": 1,
  "b": 2
}

Expected behavior: I’d like a set of input options that doesn’t make changes to the input; i.e., I want the output to also be

{
  //
  "a": 1,
  "b": 2,
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:15
  • Comments:24 (7 by maintainers)

github_iconTop GitHub Comments

6reactions
difrcommented, May 12, 2019

.prettierrc

{
...
  "overrides": [
    {
      "files": ["*.json", ".*rc"],
      "options": {
        "parser": "json5",
        "quoteProps": "preserve",
        "singleQuote": false,
        "trailingComma": "all",
      },
    },
  ],
}

😃

6reactions
ikatyangcommented, Jan 9, 2019

Personally, I think jsonc (the language named JSON with Comments in VSCode, which is also used by TypeScript tsconfig.json) is better than JSON5 since it only adds missing features that are necessary so it’s relatively simple.

I was thinking of adding a --parser jsonc to address this issue, but then I found that its name (JSON with Comments) is very confusing since it looks like JSON + comments but it’s actually JSON + comments + trailing commas while our --parser json already handles JSON + comments, so the jsonc description may look something like “JSON with Comments: same parser as json, but trailing commas are allowed”, which is super confusing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can you use a trailing comma in a JSON object?
Unfortunately the JSON specification does not allow a trailing comma. There are a few browsers that will allow it, but generally you need...
Read more >
Why doesn't JSON allow trailing commas? Why? How hard is ...
It doesn't dictate how it's used, one of its goals is to be human-readable. Not allowing trailing commas is a JS thing historically....
Read more >
JsonSerializerOptions.AllowTrailingCommas Property
By default, AllowTrailingCommas is set to false , and a JsonException is thrown during deserialization if a trailing comma is encountered.
Read more >
Best practices for using trailing commas in JavaScript
The JSON file format was introduced in the early 2000s. Since JSON is based on JavaScript's object syntax, and it was invented before...
Read more >
json parse alternative that it not strict with trailing commas
This works because eval is actually executing that as Javascript, which ia where the trailing comma thing is valid. Thats why we need...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found