tags:

views:

1730

answers:

7

When manually generating a JSON object or array, it's often easier to leave a trailing comma on the last item in the object or array. For example, code to output from an array of strings might look like (in a C++ like pseudocode):

s.append("[");
for (i = 0; i < 5; ++i) {
    s.appendF("\"%d\",", i);
}
s.append("]");

giving you a string like

 [0,1,2,3,4,5,]

Is this allowed?

+8  A: 

No. The JSON spec, as maintained at json.org, does not allow trailing commas. From what I've seen, some parsers may silently allow them when reading a JSON string, while others will throw errors. For interoperability, you shouldn't include it.

The code above could be restructured, either to remove the trailing comma when adding the array terminator or to add the comma before items, skipping that for the first one.

Ben Combee
+1  A: 

In general I try turn the problem around, and add the comma before the actual value, so you end up with code that looks like this:

s.append("[");
for (i = 0; i < 5; ++i) {
  if (i) s.append(","); // add the comma only if this isn't the first entry
  s.appendF("\"%d\"", i);
}
s.append("]");

That extra one line of code in your for loop is hardly expensive...

Another alternative I've used when output a structure to JSON from a dictionary of some form is to always append a comma after each entry (as you are doing above) and then add a dummy entry at the end that has not trailing comma (but that is just lazy ;->).

Doesn't work well with an array unfortunately.

brianb
A: 

Interestingly, both C & C++ (and I think C#, but I'm not sure) specifically allow the trailing comma -- for exactly the reason given: It make programmaticly generating lists much easier. Not sure why JavaScript didn't follow their lead.

James Curran
ECMA has explicitly specified that trailing commas are allowed in the upcoming spec: http://ejohn.org/blog/bug-fixes-in-javascript-2/Yet another reason to be clear that JSON != JS Object.
eyelidlessness
Trailing commas work in Firefox, but not in IE.
kzh
+2  A: 

PHP coders may want to check out implode(). This takes an array joins it up using a string.

From the docs...

$array = array('lastname', 'email', 'phone');
echo implode(",", $array); // lastname,email,phone
rikh
A: 

I usually loop over the array and attach a comma after every entry in the string. After the loop I delete the last comma again.

Maybe not the best way, but less expensive than checking every time if it's the last object in the loop I guess.

Nils
+3  A: 

For all those bitching that someone asked a simple question, please back off. This was one of the first hits on google, and it helped me reference the answer quickly. Thank you OP.

anon
For what it's worth, my complaint wasn't that the question is simple (I've gladly answered a number of simple questions here), but that it appeared (at first glance) to be a question aimed at gaming reputation points. You'll also note that I conceded the question is valid after some discussion.
eyelidlessness
A: 

Trailing commas work in Firefox, but not in IE. – kzh May 11 at 15:15

Bump! (i don't expect points)

negutron
Please don't "bump" things on SO; it isn't a forum. Instead, just upvote comments/answers you like! :-)
Ben Blank