Forum Discussion

chrigu's avatar
chrigu
Guide
10 years ago

REST response to JSON

Hi there

 

I'm building an endpoint and I would like to output the response of the following REST call as plain JSON:

 

<#assign ideaBoards = rest("/categories/id/mycategory/boards/style/idea?restapi.response_style=view") />

 

Is there way to convert ideaBoards to a JSON string? Or to a normal Hash?

 

Note: I'm aware that I can calll rest("/categories/id/mycategory/boards/style/idea?restapi.response_format=json") to get a JSON response directly, but I would like to include the response above in my JSON response e.g.:

 

{

  "some": "Some other Text",

  "other":[12,3,4],

  "ideaBoards":ideaBoards?toJSON

}

 

Thanks for any hints!

chris

 

  • You can get json response by appending parameter ?xslt=json.xsl in REST call.

     

    E.g.: 

    <#assign ideaBoards = rest("/categories/id/mycategory/boards/style/idea?restapi.response_style=view&xslt=json.xsl") />

     

    • chrigu's avatar
      chrigu
      Guide

      phani 

       

      Thanks for the reply, but we woud like to filter some of the returned data and then return it as JSON. This means we would like to convert a XML node to JSON.

       

      Currently we do something like:

       

      <#assign cleanPost = {
      "postLink": post.@view_href?string,
      "postKudos": post.kudos.count?number,
      "postPostTime": post.post_time.@view_time?string,
      "postPostDate": post.post_time.@view_date?string,
      "postTeaser": utils.html.stripper.from.gdata.strip(post.teaser?string)?json_string,
      "postViews": post.views.count?string,
      "postSubject": post.subject?json_string,
      "postAuthor": post.author.login?json_string
      }
      />

       

      and then orginise our data in arrays and dictionaries and, finally, use this conversion macro to return the data.