The dreaded "invalid JSON string for Date type.

366 views
Skip to first unread message

Steve Sether

unread,
May 17, 2012, 6:05:33 PM5/17/12
to Google Visualization API
I'm getting this error message as soon as I put my json data into the
DataTable. I've searched the forums and found several others that had
this problem, but none of these solutions worked (I've even used the
data they posted with the corrections instead of my own, still the
same error.)

I'm at a loss right now. My json is below, and was generated using
the Google java lib. If I use data without a date type, everything
works fine.

the code is simply:
var jsonData = $.ajax({
url: "data2.json",
dataType:"text",
async: false
}).responseText;

var data = new google.visualization.DataTable(jsonData);


data2.json is simply:

{"cols":[{"id":"date","label":"date","type":"date","pattern":""},
{"id":"ColA","label":"ColA","type":"number","pattern":""},
{"id":"ColB","label":"ColB"
,"type":"number","pattern":""}],"rows":[{"c":[{"v":new Date(2001,0,1)},
{"v":551.0},{"v":422.0}]},{"c":[{"v":new Date(2001,0,2)},{"v":521.0},
{"v":422.0
}]},{"c":[{"v":new Date(2001,0,3)},{"v":430.0},{"v":422.0}]},{"c":
[{"v":new Date(2001,0,4)},{"v":399.0},{"v":422.0}]},{"c":[{"v":new
Date(2001,0,5)},{
"v":402.0},{"v":336.0}]},{"c":[{"v":new Date(2001,0,6)},{"v":336.0},
{"v":336.0}]},{"c":[{"v":new Date(2001,0,7)},{"v":474.0},{"v":336.0}]},
{"c":[{"v":
new Date(2001,0,8)},{"v":527.0},{"v":336.0}]},{"c":[{"v":new
Date(2001,0,9)},{"v":462.0},{"v":336.0}]},{"c":[{"v":new
Date(2001,0,10)},{"v":315.0},{"v
":336.0}]}]}


Please help!

asgallant

unread,
May 18, 2012, 9:44:09 AM5/18/12
to google-visua...@googlegroups.com
Your dates are input improperly.  They need to be strings in the format:

"Date(2001,0,1)"

notice there is no "new" keyword.  Your JSON should look like this:

{"cols":[{"id":"date","label":"date","type":"date","pattern":""},{"id":"ColA","label":"ColA","type":"number","pattern":""},{"id":"ColB","label":"ColB","type":"number","pattern":""}],"rows":[{"c":[{"v":"Date(2001,0,1)"},{"v":551.0},{"v":422.0}]},{"c":[{"v":"Date(2001,0,2)"},{"v":521.0},{"v":422.0}]},{"c":[{"v":"Date(2001,0,3)"},{"v":430.0},{"v":422.0}]},{"c":[{"v":"Date(2001,0,4)"},{"v":399.0},{"v":422.0}]},{"c":[{"v":"Date(2001,0,5)"},{"v":402.0},{"v":336.0}]},{"c":[{"v":"Date(2001,0,6)"},{"v":336.0},{"v":336.0}]},{"c":[{"v":"Date(2001,0,7)"},{"v":474.0},{"v":336.0}]},{"c":[{"v":"Date(2001,0,8)"},{"v":527.0},{"v":336.0}]},{"c":[{"v":"Date(2001,0,9)"},{"v":462.0},{"v":336.0}]},{"c":[{"v":"Date(2001,0,10)"},{"v":315.0},{"v":336.0}]}]}  

Steve Sether

unread,
May 18, 2012, 1:13:26 PM5/18/12
to Google Visualization API
Thanks. This worked.
Reply all
Reply to author
Forward
0 new messages