Forum Discussion

Gursimrat's avatar
Gursimrat
Leader
11 years ago

Iterate through JSON response in FTL

Hi,

 

I am getting a response in my endpoint via http.client.request in the JSON format, I want to iterate thorugh the nodes of the json via FTL, but I am not able to.

 

Code to read the json:

 

<#assign search_query = http.request.parameters.name.get("search_query","test") />

<#assign response =  http.client.request("http://external-search/collection1/select?q=${search_query}&wt=json&indent=true").get() />
<#if response.hasError>
${response.error.message}
<#else>

<#assign response2 = response.content.docs>
<#list response2 as result>
${result.summary}
</#list> </#if>

 

 

The JSON is as below:

 

{
   "response":{
      "start":0,
      "docs":[
         {
            "summary":" results community ",
            "body":"Array results community Re: results community Re: results community",
            "votecount":0,
            "_version_":1463544135618134016,
            "noOfComments":2,
            "lastmodified":"2014-03-08T06:06:07&#43;00:00",
            "type":"board",
            "url":"/threads/id/149",
            "id":"149",
            "title":"results community",
            "postedby":"Varun",
            "sectionlabel":"Using results",
            "sectionid":"using-results"
         },
         
         {
            "summary":" New threads in the results communityNew threads in the results community ",
            "body":"Array New Message Re: New Message",
            "votecount":0,
            "_version_":1463544135667417088,
            "noOfComments":1,
            "lastmodified":"2014-02-25T18:42:46&#43;00:00",
            "type":"board",
            "url":"/threads/id/83",
            "id":"83",
            "title":"New Message",
            "postedby":"Varun",
            "sectionlabel":"Testing Stories",
            "sectionid":"testing-stories"
         }
      ],
      "numFound":42
   },
   "responseHeader":{
      "status":0,
      "QTime":1,
      "params":{
         "indent":"true",
         "q":"results",
         "wt":"json"
      }
   }
}

 Giving error:

 

{ "error":"
FreeMarker template error:
For "." left-hand operand: Expected a hash, but this evaluated to a string (wrapper: f.t.SimpleScalar):
==> response.content  [in template "zendesksearchapiurl.ftl" at line 8, column 22]

The failing instruction (FTL stack trace):
----------
==> #assign response2 = response.content...  [in template "zendesksearchapiurl.ftl" at line 8, column 1]
    #else  [in template "zendesksearchapiurl.ftl" at line 6, column 1]
    • Gursimrat's avatar
      Gursimrat
      Leader

      NicoB 

       

      This is not working when I user .json()

       

      .get() is Working

       

      <#assign search_query = http.request.parameters.name.get("search_query","optimizely") />
      
      <#assign response =  http.client.request("http://community/collection1/select?q=${search_query}&wt=json&indent=true").get() />
      <#if response.hasError>
      ${response.error.message}
      <#else>
      ${response.content}
      </#if>

       

       

       

      .json() is NOT Working

      <#assign search_query = http.request.parameters.name.get("search_query","optimizely") />
      
      <#assign response =  http.client.request("http://community/collection1/select?q=${search_query}&wt=json&indent=true").json() />
      <#if response.hasError>
      ${response.error.message}
      <#else>
      ${response.content}
      </#if>

       

      Error is as below:

       

      { "error":"
      FreeMarker template error:
      The following has evaluated to null or missing:
      ==> response.hasError  [in template "zendesksearchapiurl.ftl" at line 4, column 6]
      
      Tip: If the failing expression is known to be legally null/missing, either specify a default value with myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthessis: (myOptionVar.foo)!myDefault, (myOptionVar.foo)??
      
      The failing instruction (FTL stack trace):
      ----------
      ==> #if response.hasError  [in template "zendesksearchapiurl.ftl" at line 4, column 1]
      ----------
      • NicoB's avatar
        NicoB
        Lithium Alumni (Retired)

        Hi Gursimrat 

        the right call should be

         

        <#assign response =  http.client.request("http://community/collection1/select?q=${search_query}&wt=json&indent=true").json().get() 
        

         

        Thanks,

        Nico