Forum Discussion
There may be a simpler solution, but you could use a macro to convert the Freemarker object to JSON, for example:
http://ericbrandel.com/2013/03/28/freemarker-container-to-json/
Thank you. I'll have to try it to be sure, but I don't see where this macro inserts quotes around strings in the resulting JSON, and I don't see any escaping happening, so I don't think the JSON would be safe to use in JavaScript code if the data was tainted and unescaped.
Lithium Engineering: Please add a native options for escaped JSON string output from the internal rest() function.
- YuriK12 years agoKhoros Expert
Hey Shawn-ad,
You can do the following (Thanks DougS) in an endpoint (but not in components):1) Create an endpoint with your rest call similar to the following (Note: the @@markup call will get you the xml string for the freemarker object):
${rest("/threads/recent").@@markup}
2) Set return type to text/plain
3) call your endpoint with the following query parameter
?xslt=json.xsl
This should return the json as a string.
Hope this helps,
Yuri
- shawn-ad12 years agoGuide
Thanks, Yuri. It is interesting that I can use your suggestion to convert any XML emdpoint into JSON. However, I was trying to get JSON data rendered inside the page HTML so that I don't have to make a separate HTTP request to the API (or to a custom endpoint).
Clearly Lithium has a way to get an API response as XML markup (using @@markup an endpoint). it would be great if I could get JSON using a similar FreeMarker translation method, and do so in a component to get it rendered in-page.
- luk10 years agoBossbig +1000, the problem is not to get JSON from an endpoint but inside a component or any type of freemarker code...the rest() lithium context object ALWAYS returns a freemarker node when used in a freemarker template, this makes things much harder as they would need to be =/
Related Content
- 4 years ago
- 5 years ago