Date objects in JSON

hingo's picture

Flavio of MongoDB fame complained in a tweet about wanting a date format in JSON. Currently you can only express strings and numbers as the primitive data types in JSON, as well the composite forms of a list: [] and an object: {}. The latter is essentially just a subhierarchy in the object/array that is your full JSON document.

i wonder why there's no "STANDARD" way to (de)serialize times / date times into / from json! They might not be "primitive" but COME ON!!

— FlaPer87 (@flaper87) July 20, 2012

My response:

@flaper87 Going back to basics: is there a json-like string that you can eval() and get a JS Date object?

— Henrik Ingo (@h_ingo) July 20, 2012

What did I mean by that?

Json serialization was originally invented in JavaScript circles. The point of the Json format is that it is valid JavaScript syntax. Originally, de-serialization was done with the eval() function:

<script type="text/javascript">
var jsondoc = '{"hello" : "world", "foo" : "bar", "nr" : 1 }'
var obj;
eval("obj = " + jsondoc);

So my point is, whatever date-format you want to standardize for Json, it needs to at least be valid JavaScript syntax, so that a user could eval() the string and actually get a Date() object. Something like a simple:

var d = new Date("2012-07-22T19:49:00");

In fact, that is perfectly possible (tested on Firefox) to put into a json-like string:

<!DOCTYPE html>


<p id="one">A Paragraph.

<p id="two">A Paragraph.

<p id="three">A Paragraph.

<p id="four">A Paragraph.

<p id="five">A Paragraph.

<p id="six">A Paragraph.

<p id="seven">A Paragraph.

<script type="text/javascript">

var d;
d = new Date("2012-07-22T19:49:00");

var jsondoc = '{"hello" : "world", "foo" : "bar", "nr" : 1, "timestamp" : "2012-07-22T19:49:00", "dateobj_ref" : d, "dateobj_inline" : new Date("2012-07-22T19:49:00") }'
var obj;
eval("obj = " + jsondoc);



So there you have it. The JSON notation for a date type needs to be simply:

jsondate = '{ "dateobj" : new Date("2012-07-22T19:49:00") }'


It turns out at least newer versions of Firefox JavaScript also support a newer Date.toJSON() function that will return a string like 2012-07-22T16:49:00.000Z. How this is supposed to be JSON I don't really know. If I would put that in the place of a value in a json document, it is not valid JavaScript syntax. If I put it into quotes, it of course becomes a string.

But the new Date(...) approach actually works.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Use [fn]...[/fn] (or <fn>...</fn>) to insert automatically numbered footnotes.
  • Allowed HTML tags: <h1> <h2> <h3> <h4> <p> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <sup> <div> <blockquote> <pre> <img>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically. (Better URL filter.)

This question is for testing whether you are a human visitor and to prevent automated spam submissions.