Forum Discussion

pp_01's avatar
pp_01
Mentor
8 years ago

Using freemarker for getting a URL (reading JSON) from a local JSON asset.

Hi. I'm new in freemarker and I'm facing some issues while using it in a page I'm creating. I am trying to use a select element in my page and the functionality I want is that whenever a user clicks on any of the countries from that dropdown it should take the URL from the JSON in which I have a key and value, "country" and "URL" for that country respectively and it should open to a new page for the specific country. I have tried to do this through Javascript and it's working but I'm not getting the proper freemarker needed for this. Let's suppose the JSON I'm using is this -

"countries": [
        {
            "name": "Argentina",            
            "url": "https://argentina/events/"
            },
        {
            "name": "Australia",
            "url": "https://australia/events/"
            }
]

What are the required things to be done for getting this functionality? If any one can help that would be awesome. Thanks in advance.

  • Hi pp_01

     

    You should be able to store the JSON object against a Freemarker variable that will create a hashmap and allow you then to access the properties.

     

    There are several ways you can do this but I've found this blog article - Working with JSON in Freemarker covers them quite well.

     

    Let us know how you get on.

    • pp_01's avatar
      pp_01
      Mentor

      Thank you for answering Robert  :smileyhappy: . I have looked at the link you mentioned and have tried something on my page.  It shows no error in the preview but on the page, I'm not getting the required functionality. In the select element, options are there but nothing is happening when I'm clicking on them. Is there anything that I'm forgetting? Apart from this what is needed in the code so that I can read the JSON if I've uploaded it in assets of my dev environment? How can I get the URL from the JSON? Do I need to use any REST call for that?

      <#assign countries =
      		[
      			{
                                     "name":"Argentina",
                                     "url":"https://testurl.com"
                              },
                              {
                                     "name":"Australia",
                                     "url":"https://testurl.com"
                              }
                      ]		  
      > <select name="countrySelect" id="countrySelected"> <#list countries as country> <option value ="${country.url}" target="_blank">${country.name}</option> </#list> </select>

       

      • Payal's avatar
        Payal
        Director

        Hi pp_01,

         

        You have to use onchange attribute in select statement as following:

         

        <select name="countrySelect" id="countrySelected" onchange="location = this.value;">
        <#list countries as country>
        <option value ="${country.url}" target="_blank">${country.name}</option>
        </#list>
        </select>

         

        LMK if this works for you.

         

        Thanks,

        Payal