4

I am trying to the timeline chart: http://code.google.com/apis/visualization/documentation/gallery/annotatedtimeline.html#Data_Format Data is coming in the form of a JSON feed.

Google wants the data as something like this:

    {
   version:'0.6',
   reqId:'0',
   status:'ok',
   sig:'4641982796834063168',
   table:{
      cols:[
         {
            id:'A',
            label:'NEW A',
            type:'string'
         },
         {
            id:'B',
            label:'B-label',
            type:'number'
         },
         {
            id:'C',
            label:'C-label',
            type:'datetime'
         }
      ],
      rows:[
         {
            c:[
               {
                  v:'c'
               },
               {
                  v:3.0,
                  f:'3'
               },
               {
                  v:new Date(2008,
                  3,
                  30,
                  0,
                  31,
                  26                  ),
                  f:'4/30/08 12:31 AM'
               }
            ]
         }
      ]
   }
}

How can I output the Date function without it being wrapped in string delimiters like 'Date()'.

2
  • 1
    Since this is related to google charts I found out they do allow a Date string in the format "Date(yyyy, mm, dd[,hh,ss,ii])" The json charts requires is invalid because they use different quotation marks. Commented Jun 30, 2010 at 3:51
  • Yes that works! and is the simplest solution. i.e. simply remove the word 'new'. Keyo Where did you find out the string Date specified? I cannot see it in the Charts API? (I'd like to know if it's official and will remain). Commented Sep 21, 2014 at 1:10

3 Answers 3

4

I simply wrapped the functions in %% characters like so:

$something = array('%%new Date(...) %%','somevalue');
$json = json_encode($something);

And removed these %% characters and the string delimiters next to them.

$json = preg_replace("/(('|\")%%|%%(\"|'))/",'', $json);
Sign up to request clarification or add additional context in comments.

1 Comment

Note that this will only work in a very narrow usecase. Some folks might land here that want to transmit JS within JSON via ajax etc.. Note that transmiting js in JSON will a) not work by default (you'd need to eval the response) and b) it's not meant to be used like that. One might only use the proposed solution here if its used within output of a javascript resource file/script tag.
1

Unfortunately, there is no "date literal" in JavaScript (arrays can be expressed with [] and objects with {}, but no such love for Date objects). As well, actual, valid JSON only accepts primitive values (like strings, numbers, arrays, booleans, objects and not much else). You may also be surprised to learn that the Date() in JSON is not valid (though that's not a problem if you don't care about portability).

If you have control over the code that produces the feed and consumes it, though, you may want to do one of a couple things. First, pass the date as a timestamp. This is easy enough:

var dtDateTime = new Date('Jan 27 2011 00:00:00 GMT+0000');
var intDateTime = dtDateTime.getTime();
var objJSON = {
    "datetime":intDateTime
};

After loading the JSON, your code would then parse the datetime with:

var dtDateTime = new Date();
dtDateTime.setTime(objJson.datetime);

Here, your code would have to expect the datetime property and know to decode it. So it's not a great generalized solution.

Another way I've seen this nut cracked is with a special string literal, that signifies to your script that it is a datetime. It could be in the value:

var objJSON = {
    "datetime":"@Jan 27 2011 00:00:00GMT+0000@"
};

Or it could be name:

var objJSON = {
    "@datetime":1296086400000
};

The @ simply acts as a flag to your code that the value needs some sort of post-processing. Both will pass validation.

JSON is meant to be simple and cross-platform, so anything that is specific to JS is inherently bad. If you tried to load JSON into, say Java or C#, you'd have a problem.

Comments

0
    $data = preg_replace('@new Date\(([^\)]*)\)@', '"$1"', $data);
    $data = json_decode($data, true);

Result:

new Date(2008,3,30,0,31,26) => "2008,3,30,0,31,26"

Comments

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.