I have json object. I want to parse it in sightly JS. Which API to use ?
Solved! Go to Solution.
Views
Replies
Total Likes
Views
Replies
Total Likes
If you have a JSON string you can use:
var object = JSON.parse(jsonString);
And then use the object to refer to the properties.
If you are using JS-Use API then its ultimately JS at the end and parsing it is nothing but simply using the keys to get desired value out of JSON.
If you are using Java-Use API then the one I have been using to map JSON to java objects is GSON. GSON is pretty handy in converting JSON to JAVA Object and vice-versa.
- Runal
Views
Replies
Total Likes
Views
Replies
Total Likes
This doesn't work for me, I have tried this following is js code
"use strict";
use(function() {
var pages = {};
pages.text = properties.get("pages");
pages.ctas = JSON.parse(properties.get("pages"));
return pages;
});
When I call this in html
<div data-sly-use.pages="card.js">
<p> Json text ${pages.text}</p>
<p> CTAs : ${pages.ctas}</p>
</div
I get ${pages.text} displayed however ${pages.ctas} throws exception in error.log
07.07.2015 18:06:41.554 *ERROR* [0:0:0:0:0:0:0:1 [1436306801283] GET /content/veritas/en/testpage.html HTTP/1.1] com.day.cq.wcm.core.impl.WCMDeveloperModeFilter Error during include of SlingRequestPathInfo: path='/content/veritas/en/testpage/jcr:content/par/card', selectorString='null', extension='html', suffix='null'
org.apache.sling.api.SlingException: Cannot get DefaultSlingScript: org.mozilla.javascript.EcmaError: SyntaxError: Unexpected token: L (/apps/veritas/components/content/card/card.html#5)
Views
Replies
Total Likes
Can you print what is inside properties.get("pages")?
thanks,
feike
Views
Replies
Total Likes
Use Handlebars. There's no explicit api to parse json directly in sightly. Instead you can dump the json in a handlebar template which can display the json data.
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies