cancel
Showing results for 
Search instead for 
Did you mean: 

Controller and JSON

sihnu
Champ in-the-making
Champ in-the-making
Hi,

in our current project we have created a webscript that starts a workflow by definition name. The webscript works fine. Now problem is that there are some bunch of properties that should be multiplied. We thought different scenarios to solve this problem and finally we came up with a solution where we send the collection of properties (lets say it was employee with properties name, address and age) as json object. Now the idea is to create a custom controller that parses and iterates the json creating fields for every item in the list (for every employee).

I managed to send json as a property but now I'm having problems with json parsing in freemarker template of my controller.I have googled for the solution and I stumbled couple of times in simple solution that seems to work fine for those ppl.


// assign json to employeelist and loop it like:

<#list employeelist as emp>
${emp.name}


But this doesn't work in my solution.I get error "Expected collection or sequence…" So it seems freemarker is not seeing the property as json object but rather as plain string. How can I tell freemarker that it is actually iterable json-object (a list)? I've tried to google with keywords "freemarker convert string to json" but no luck so far. This is small thing but really preventing me from continuing. Btw, here is the json I use to test:


[{"name": "Fred Bloggs", "email": "fred@test.com", "id": "123456"},{"name": "John Smith", "email": "john@test.com", "id": "456789"},{"name": "Bob Brown", "email": "bob@test.com", "id": "987654"}]


(and the value comes to the controller like that. I had to escape the " marks as \" though but the json comes without the escapes to the controller (like above). I tested it.

I would be really happy if someone could help me here. Thanks in advance.
9 REPLIES 9

muralidharand
Star Contributor
Star Contributor
Hi,
The "EmployeeList" is a plain string and it is not a collection and as far I know, you can not convert string to JSON in FTL. Otherwise, you need to write a separate library to convert the string to JSON object.
I would recommend, from the controller side, you should be able to convert the string to JSON (in Both Javascript controller + Java controller) and do the iteration in the FTL.


Please let me know, if you're not clear.

Thanks
Murali

sihnu
Champ in-the-making
Champ in-the-making
Hi,

thanks a lot for the response. Okay, I know how to convert json in javascript but how do I iterate the object in freemarker? I would rather do it in freemarker than in javascript.

Here is my script:


<script language="Javascript" type="text/javascript">//<![CDATA[
    var employees = json.parse(${field.value});
//]]></script>


I guess this should work for parsing to json object in javascript.

Hi,
Where are you trying to convert json string to JSON object ?
In Share side or Repo side?

sihnu
Champ in-the-making
Champ in-the-making
Hey, thanks for the reply again.

I'm trying to do it at share (client) side. The thing is that custom controllers are not so familiar to me. I know I get the json as field.value (one of the fields in the form uses the controller, defined in share-config-custom.xml). I wonder if I can create a javascript file for the controller that could be ran in server side. Do the controllers work the same way as webscripts? How would I run the javascript? How should I tell that to Alfresco (naming of the file etc.)? I know this post is now out of topic. I'm sorry about that.

Hi,
To know more about webscript, please use the below uris.

https://wiki.alfresco.com/wiki/Web_Scripts
https://wiki.alfresco.com/wiki/Web_Scripts_Examples

There are already out of box share webscripts are available with similar approach.

The Share webscript reads the data from repository and do the iteration in the html.ftl file rather than the json.ftl file.
People prefers .html.ftl to bind the data directly to the page.
For ex, dashlets are built by using webscripts. It takes the data from the repository and renders it directly in the dashlets and few dashlets calls the repo webscript directly from the client-side javascript and renders the data into dashlets.

My Tasks Dashlet:
  It uses the AJAX request (my-tasks.js) to get the data from the repository and it uses the YUI datatable to render the data.

Site Members Dashlet:
It reads the data from repository by using "colleagues.get.js" to read the data and it uses "colleagues.get.html.ftl" file to render the site members.

Controllers:
Controller is a part of webscript.

Running Javascript.
If it is client side javascript, you need to include the javascript in the .headd.ftl file.
If it is a controller javascript, follow the proper webscript naming convention to execute the server side javascript file.

Please let me know, if you're not clear.


Try <#assign jsonVariable = string_variable?eval>

sihnu
Champ in-the-making
Champ in-the-making
Hi,

this is exactly what I wanted and now it works! Thanks a lot. I already created webscript for converting json to html where I first "converted" String to JSON and returned HTML However, I got the same error as before. Freemarker didn't treat the value as json object/array (the freemarker template in webscript). I think I don't need the extra webscript for converting anymore. I'm pretty sure this should work in the controller template as well.I just need to test it. Once again thank you.

muralidharand
Star Contributor
Star Contributor
Hi,
I created a very simple sample webscript for you.

Use the eval() method to convert jsonString to JSON object in the Javascript controller.
If you're using java controller, in the Java code, convert the json string to JSON object.

<strong>employee.get.js</strong>

var jsonString = '[{"name": "Fred Bloggs", "email": "fred@test.com", "id": "123456"},{"name": "John Smith", "email": "john@test.com", "id": "456789"},{"name": "Bob Brown", "email": "bob@test.com", "id": "987654"}]';

//use the eval method to convert jsonString to JSON object.

model.employeeLis = eval('(' + jsonString + ')');




<strong>employee.get.json.ftl</strong>

<#list employeeList as emp>
   ${emp.name}
</#list>



<strong>employee.get.desc.xml</strong>

<webscript>
  <shortname>example-webscript</shortname>
  <description>Simple Webscript</description>
  <url>/com/quanitcate/example</url>
  <format default="json">any</format> 
  <authentication>user</authentication> 
</webscript>



Hope this helps you !

sihnu
Champ in-the-making
Champ in-the-making
So I could create a web script that returns HTML and then I could include that to my template. I guess that could work if I can't do it straight away. Thanks, this is nicer solution than creating the fields with javascript.