0

Why is this JSON giving me parse errors? I could swear I've done this a hundred times before without issue.

[[0,0,0,new Date(1364068990245)],[0,0,0,new Date(1364068940075)]]
6
  • 13
    That's not JSON. Commented Mar 23, 2013 at 21:02
  • "I've done this a hundred times before" - oh no you haven't... Commented Mar 23, 2013 at 21:03
  • I presumed that any valid JS array or object was "JSON." Commented Mar 23, 2013 at 21:05
  • 2
    @MatthewH JSON's syntax is based on JavaScript's literals syntax, thus the name. But, it uses a strict subset, making it more restrictive. json.org Commented Mar 23, 2013 at 21:14
  • 1
    Far from all code valid in Javascript is allowed in JSON. An array literal in Javascript can for example contain code that would execute when you parse it: [(function(){alert('Oh noes!');return 0;}())]. Consider what evil things you could do with that... Commented Mar 23, 2013 at 21:43

3 Answers 3

4

If that's literal JavaScript, as in this:

var myArray = [[0,0,0.......]];

Then it should be fine, your error must be coming from somewhere else.

If it's a string that you're treating as JSON, as in this:

var myArray = JSON.parse("[[0,0,0.........]]");

Then you can't have new Date (or indeed any function call) and it should be just a number that you then parse into a date.

Sign up to request clarification or add additional context in comments.

Comments

1

Because the new operator isn't part of JSON. Your example is a valid fragment of a JavaScript file, but it correctly results in an error when parsed as JSON. Dates should be serialized as strings (ideally, as ISO-8601 strings that will be accepted by the Date constructor, but I suppose numbers are tolerable as well).

1 Comment

FYI, this might be parsed by the JSON.NET library, as its JavaScriptDateTimeConverter, when added to the serializer's settings, will output dates using the new operator like this, although it's not standard JSON: james.newtonking.com/archive/2009/02/20/…
1

That's not JSON, there are no dates in the JSON format.

Ref: http://www.json.org/

Some JSON parsers have extended the standard with this way of representing a date:

"[[0,0,0,/Date(1364068990245)/],[0,0,0,/Date(1364068940075)/]]"

If the parser that you use doesn't support that, you have to transmit them as a different data type, for example numbers, and then convert them to dates after parsing the JSON:

"[[0,0,0,1364068990245],[0,0,0,1364068940075]]"

1 Comment

The "new Date(0)" format may be parsed by the JSON.NET library, as its JavaScriptDateTimeConverter, when added to the serializer's settings, will output dates using the new operator like this, although it's not standard JSON: james.newtonking.com/archive/2009/02/20/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.