I want to get a V1 API call back in a format that I can use as JSON in Javascript in a component. How do I do that? For example:
(/categories/id/categoryName/labels)
<#assign someLabels = restadmin("/${context}/id/${contextId}/labels?restapi.response_format=json").labels.label?sort_by("text")/>
<script>
let someVar = [${someLabels}];
</script>
I know that V2 has the apiv2.toJson() method, but in this case V1 is the only way I know of to get a list of labels from a category, for example.
Solved! Go to Solution.
That didn't seem to make any difference either.
<#assign someLabels = restadmin("/categories/id/discovery/labels/labels?restapi.response_format=json").labels.label />
<script>
var labels = JSON.parse('${someLabels}');
console.log('labels',labels);
</script>
Same error.
No idea why this isn't working; I've opened a ticket with Khoros on it but thanks for the help. I'll update this thread when I get a reply from them.
@IanAtJMP - You could check if your request results in a non-empty list of labels:
<#if someLabels?? && someLabels?size gt 0>
<#-- Print ${someLabels?size} here -->
<#list someLabels as label>
<#-- Print ${label.text} here -->
</#list>
</#if>
Or you simple test your request through the browser or via other tools (curl, Postman).
Is it still the line
var labels = JSON.parse('${someLabels}');
which causes the error?
It's still the line JSON.parse() that's giving me the issue. And ${label.text} won't print because it's not actually a text node, it's an object, "$" has the text ... whew. Too confusing. Thanks tho.
The "$" sign shouldn't bother you. You can access the text or id value of labels as I mentioned. Freemarker handles this for you.
Did you try the snippet I provided to test your someLabels variable?
If this snippet works and will print some data, you can be sure your request/response is correct and there are maybe some limitations on accessing the object as you tried in your JS code.
I did use the snippet but it didn't print anything. When I removed the comment marks, did print in the enpdoint. Still can't get it into JS tho.
@IanAtJMP - Maybe we should go a step back and see if I understand your scenario in the right way 😊
You fetch labels of a node in an endpoint and want to access them via JS from a custom component, is that correct?
If so, I recommend the following:
If I'm wrong, it would be great if you could describe your intention, so I can figure out how to go on. 🙂
@cike Thanks, yes, that is correct.
However, the issue is that I can't get the endpoint to show up in JSON? If it's not JSON in the endpoint itself, how am I going to call that in a component and expect JSON? That make sense? Thanks 🙂
I guess the question is then, "how do I get an endpoint to return JSON"?
@IanAtJMP - Ok. Good to know that you want your endpoint to return JSON which than can be requested by your components.
You could use something like this to build your endpoint:
<#attempt>
<#assign someLabels = restadmin("/categories/id/discovery/labels? restapi.response_format=json").labels.label />
<#if someLabels?? && someLabels?size gt 0>
{
"response": {
"status": "successful",
"labels" : [
<#list someLabels as label>
"${label.text}"<#if (label?has_next)>,
</#list>
]
}
}
<#else>
{
"response": {
"status": "empty"
}
}
<#recover>
{
"response": {
"status": "error",
"message": "${.error?json_string}",
"query" : "${queryMsg?json_string}"
}
}
This should always return a JSON object from your endpoint. You should test your endpoint by directly accessing its Url, to see if things work as expected.
Thanks! I just added a few things to it to get it to work 100%:
<#attempt>
<#assign someLabels = restadmin("/categories/id/discovery/labels?restapi.response_format=json").labels.label />
<#if someLabels?? && someLabels?size gt 0>
{
"response": {
"status": "successful",
"labels" : [
<#list someLabels as label>
{"id":"${label.id}","text":"${label.text}"}<#if (label?has_next)>,
]
}
}
<#else>
{
"response": {
"status": "empty"
}
}
<#recover>
{
"response": {
"status": "error",
"message": "${.error?json_string}",
"query" : "${queryMsg?json_string}"
}
}
Great!
Hope everything works as you want, now. Requesting the endpoint via AJAX from a component shouldn't be that difficult from this point. 🙂
Welcome to the Technology board!
Curious about our platform? Looking to connect on social technology? You've come to the right place!