Forum Discussion

shawn-ad's avatar
11 years ago

Get JSON from internal rest() call

I'm making some REST calls within a custom component. Rather that traverse a nested response object in FreeMarker, I'd prefer to receive a JSON string, which I could insert as a data-* attribute on one of the HTML nodes in my custom component and deal with on the client-side using JavaScript. So I'd like to do something like this:

<div class="hidden my-data" data-board-topics="${
rest("/boards/id/extensions/topiclist?restapi.format_detail=full_list_element&restapi.response_format=json")
}">

However, when I append "&restapi.response_format=json" to the rest parameter, it still returns a nested response object, not a JSON string. I know I can get JSON by making an external REST API call from the client side, but is there a way to get a JSON string from the internal rest function?

    • shawn-ad's avatar
      shawn-ad
      Guide

      Thank you. I'll have to try it to be sure, but I don't see where this macro inserts quotes around strings in the resulting JSON, and I don't see any escaping happening, so I don't think the JSON would be safe to use in JavaScript code if the data was tainted and unescaped.

       

      Lithium Engineering: Please add a native options for escaped JSON string output from the internal rest() function.

      • YuriK's avatar
        YuriK
        Khoros Expert

        Hey Shawn-ad,
        You can do the following (Thanks DougS) in an endpoint (but not in components):

         

        1) Create an endpoint with your rest call similar to the following (Note: the @@markup call will get you the xml string for the freemarker object):

         

        ${rest("/threads/recent").@@markup}

         

        2) Set return type to text/plain

         

        3) call your endpoint with the following query parameter

         

        ?xslt=json.xsl

         

        This should return the json as a string.

         

        Hope this helps,

         

        Yuri