Forum Discussion

tapatalk's avatar
tapatalk
Helper
12 years ago

How to loop json data in my test endpoint?

Hello:

 

I want to know how to loop json data inside the freemarker list section.

 

I got json data from rest api in the endpoint,but when I try to get the properties of every category inside the list section I always got freemarker syntax error.

I want to get the every category's id/title/view_href,etc.But I do not know how to get them and how to inject these data items into the data property as json format data for return.

The following is my code in the endpoint.

 

 

<#compress>
<#assign messageId = http.request.parameters.name.get("id","")?string />
<#if messageId?length gt 0>
    <#attempt>
        <#assign top = rest("/categories/top?&restapi.format_detail=full_list_element&restapi.response_style=view&restapi.response_format=json").category />
        <#list top.categories as cat>
            ${cat.id} <#-- this code looks cause freemarker syntax error,maybe I made some mistake with it. -->
        </#list>
        <#assign ret = '{"response":{"status":"success", "data":{"id":${top.id}}}}'>
    <#recover>
        <#assign ret = '{"response":{"status":"error","error":{"code":100,"message":"Error making rest call with message id ${messageId}."}}}'>
        
    </#attempt>
        ${ret}
<#else>
        <#assign ret = '{"response":{"status":"error","error":{"code":100,"message":"Missing required parameter \"id\"."}}}'>
        ${ret}
</#if>
</#compress>

 

Any help would be appreciated.

3 Replies

  • YuriK's avatar
    YuriK
    Khoros Expert
    12 years ago

    Hey tapatalk,

     

    You're pretty close. When iterating in freemarker you need to get down to the list item that you want to iterate over. So just change your #list to the following and you should get the list items:

     

     <#list top.categories.category as cat>

     

    Hope this helps,

     

    Yuri

  • tapatalk's avatar
    tapatalk
    Helper
    12 years ago

    Hello Yuri:

     

    This is what I need.

     

    Thank you very much.:smileyhappy:

  • YuriK's avatar
    YuriK
    Khoros Expert
    12 years ago
    No problem, glad that worked for you!