ObservableObject.toJSON does not convert Date objects to JSON

4 posts, 0 answers
  1. Dustin
    Dustin avatar
    79 posts
    Member since:
    Oct 2012

    Posted 31 Jul 2013 Link to this post

    I have a Model object that needs converted to a JSON object for passing to a WebAPI and when I call the .toJSON method a valid JSON object is returned, but Dates are still in a Date format, which they are not being properly serialized to JSON. So I have for the meantime added a prototype method to the ObservableObject called toJSON2 that checks if the field being processed is a Date object to call the Date objects toJSON method which is a prototype method you guys created anyways. It would be nice though if this one condition to check if the field being processed is a Date to then convert to a JSON date could be added into your source code.

    kendo.data.ObservableObject.prototype.toJSON2 = function () {
        var result = {}, value, field;
        for (field in this) {
            if (this.shouldSerialize(field)) {
                value = this[field];
                if (value instanceof kendo.data.ObservableObject || value instanceof kendo.data.ObservableArray) {
                    value = value.toJSON2();
                else if (value instanceof Date) {
                    value = value.toJSON();
                result[field] = value;
        return result;

  2. Atanas Korchev
    Atanas Korchev avatar
    8462 posts

    Posted 31 Jul 2013 Link to this post

    Hi Dustin,

    The following demo http://jsbin.com/ulubuq/1/edit shows that JSON.stringify correctly serializes JavaScript dates which are part of a kendo observableobject. The toJSON method is used by JSON.stringify to get the "raw" data of an object. Could you please modify that demo to show the actual problem you are dealing with?

    Atanas Korchev
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. Nathan
    Nathan avatar
    43 posts
    Member since:
    Sep 2011

    Posted 05 Oct 2016 in reply to Atanas Korchev Link to this post

    Atanas - Dustin is correct.  I ended up here after having the same problem... toJSON DOES NOT WORK.  It does not create valid JSON for date fields.  JSON.stringify(MyObservableObject) does create JSON dates, but you can pass the result through as an MVC model.  toJSON is largely useless.  It might work with some servers, but not with c#/MVC/IIS.

    I've ended up using this.Request.Params["DateFieldName"] as parsing that into the model field.

  4. Dimiter Topalov
    Dimiter Topalov avatar
    1297 posts

    Posted 07 Oct 2016 Link to this post

    Hi Nathan,

    Can you please specify is there any Kendo UI - specific issue, related to the discussed topic, and if so - provide isolated example of what exactly the discrepancy between the expected and the actual behavior is, so we can provide a more to-the-point explanation, if one is needed.

    Thank you in advance.

    Dimiter Topalov
    Telerik by Progress
    Get started with Kendo UI in days. Online training courses help you quickly implement components into your apps.
Back to Top