Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • Sandra26 15 posts 115 karma points
    Sep 12, 2019 @ 15:02

    Serializing Umbraco.Content(id) to JSON

    Hello , I'm New to Umbraco and I want to create Web Api. So I need help how to serialize IPublishedContent in JSON.

  • Louis Jackson-Rees 107 posts 340 karma points c-trib
    Sep 12, 2019 @ 15:44
    Louis Jackson-Rees

    With Newtonsoft.Json you can:


    But i have done this in the past and it often crashes as the IPublishedContent object is so large, i would advise mapping the IPublishedContent item to a custom model manually or using models builder, and then serialise that model instead.

  • Steven Baker 7 posts 77 karma points
    Sep 12, 2019 @ 15:47
    Steven Baker

    Hi Sandra,

    Best to use the UmbracoApiController class to manage a transformation of IPublishedContent as the object is too complex for JSON deserialization. You can map it to a less complex object and expose it through the controller. The controller already has access to the Umbraco Helper class which will allow you to do things link query IPC.

    Look at the documentation here:

    Good luck,


  • Lee Kelleher 3888 posts 14667 karma points MVP 9x admin c-trib
    Sep 12, 2019 @ 16:00
    Lee Kelleher

    Hi Sandra26,

    As the others say, out-the-box, serializing IPublishedContent object will throw various errors. The only way to do it is with a custom resolver for JSON.NET.

    See this thread for the code...

    Then you should be able to do something like this...

        var settings = new JsonSerializerSettings
            ContractResolver = new PublishedContentContractResolver()
        var serialized = JsonConvert.SerializeObject(l, Formatting.Indented, settings);

    Hope this helps.

    - Lee

  • Sandra26 15 posts 115 karma points
    Sep 16, 2019 @ 09:54

    Thank you very much for your answers, they were really helpful. You all have a nice day , Sandra

Please Sign in or register to post replies

Write your reply to: