Skip to content

Added examples of typical errors in JSON #3564

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Alexandre887
Copy link
Member

@Alexandre887 Alexandre887 commented Aug 25, 2023

JSON methods, toJSON

let json = `{
  *!*name*/!*: "John",                      // mistake: property name without quotes
  "surname": *!*'Smith'*/!*,                // mistake: single quotes in value (must be double)
  *!*'isAdmin'*/!*: false,                  // mistake: single quotes in key (must be double)
  "birthday": *!*new Date(2000, 2, 3)*/!*,  // mistake: no "new" is allowed, only bare values
@@"gender": "male"*!* */!*                  // mistake: missing comma after non-last property @@
@@"friends": [0,1,2,3]*!*,*/!*              // mistake: there should not be a comma after the last property @@
}`;

In practice, many novice developers, following the rule that in JavaScript there can be a "trailing comma" in an object, try to reproduce the same in JSON. But a trailing comma in JSON makes it invalid. This is a very widespread stuff, so I think we should add such an example to "typical mistakes".

Also, many people forget that a comma should be after each property except the last one, which also causes JSON to become invalid. I also added such an example.

@Alexandre887 Alexandre887 changed the title Missing comma in the example Added examples of typical errors in JSON Aug 28, 2023
@smith558 smith558 self-assigned this Feb 14, 2024
@smith558 smith558 added the P1 High priority label Feb 14, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
P1 High priority
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants