6

So I've been reading up on proposed JavaScript features, and one I'm concerned about is trailing comma support in object literals and arrays.

For parameters, trailing commas don't relate here, so let's ignore that. I understand the Version Control benefits, but I'm worried how it will react to JSON.

const arr = [
    'red',
    'green',
    'blue',
];

This would become valid.

But what will happened when you return JSON syntax? JSON is supported by RFC, so I doubt JSON will ever support trailing commas. Maybe one day..

But how will JavaScript handle returning something like:

const jsonReturn = [{
    "derp":1
}, {
    "foo":"bar"
}, {
    "slide":true,
},];

Will the trailing comma be removed internally if the header content type is JSON or will trailing commas break everything?

Rob
  • 14,746
  • 28
  • 47
  • 65
Sterling Archer
  • 22,070
  • 18
  • 81
  • 118
  • 1
    Short answer: Trailing commas are invalid JSON, but most JS parsers jump over them (when interpreted as JavaScript _not_ JSON). – Brad Christie Mar 28 '16 at 15:25
  • Right, so "technically" (I use that term as loosely as possible here) you're saying that if a trailing comma is returned as JSON, the parser should just skip it and still return valid JSON to the requester? – Sterling Archer Mar 28 '16 at 15:27
  • 2
    You're not really using JSON here, you're referencing JSOs. If what you have here were to ever hit a JSON parser, it would vomit. However, dumping it out as if it were a JSO it should be fine (by most JS parsers). – Brad Christie Mar 28 '16 at 15:28
  • 1
    JavaScript is based in the English language, but new words like "selfie" showing up in the Webster dictionary will not break your scripts. Because they're entirely different things. Just like JavaScript and JSON. – Álvaro González Mar 28 '16 at 15:32
  • I think I conveyed my question poorly -- I more meant (because I know JS != JSON) would returning data from JS as JSON format with trailing commas. But, the answers and comments make sense. Thanks guys! – Sterling Archer Mar 28 '16 at 15:33
  • See: https://stackoverflow.com/questions/201782/can-you-use-a-trailing-comma-in-a-json-object – Tor Klingberg May 30 '17 at 14:46

1 Answers1

8

You won't run into any problems, because JSON and JS source have nothing to do with each other.

JSON does not (and for the sake of example, will not) support trailing commas. The current JSON spec clearly shows that commas may only occur between values within an object or array.

If JS does introduce support for trailing commas, the source representation of the object and the version that is serialized are largely unrelated. Most browsers today will accept a trailing comma, but all commas are discarded in the actual object (dict/hash or struct) representation:

> var foo = {bar: 1, baz: 2,};
< undefined
> foo
< Object {bar: 1, baz: 2}

Even today, serializing an object with a trailing comma works just fine:

> JSON.stringify({bar: 1, baz: 2,})
< "{"bar":1,"baz":2}"

The commas are for parsing source only and do not exist in the runtime's object representation.

ssube
  • 47,010
  • 7
  • 103
  • 140
  • Would that apply the same to a trailing comma in an array of objects? – Sterling Archer Mar 28 '16 at 15:31
  • 1
    @SterlingArcher absolutely. Neither objects nor arrays actually use the comma once they've been instantiated. It's purely for the parser and the parser can choose whether to recognize trailing commas or not. IE does not, most other browsers do. – ssube Mar 28 '16 at 15:32