Khoros Community

Respected Contributor
Respected Contributor

Re: Getting an API V1 call into JSON I can use in Javascript?

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. 

0 Kudos
Reply
Loading...
Honored Contributor Honored Contributor
Honored Contributor

Re: Getting an API V1 call into JSON I can use in Javascript?

@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?

0 Kudos
Reply
Loading...
Highlighted
Respected Contributor
Respected Contributor

Re: Getting an API V1 call into JSON I can use in Javascript?

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.

0 Kudos
Reply
Loading...
Honored Contributor Honored Contributor
Honored Contributor

Re: Getting an API V1 call into JSON I can use in Javascript?

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.

0 Kudos
Reply
Loading...
Respected Contributor
Respected Contributor

Re: Getting an API V1 call into JSON I can use in Javascript?

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.

0 Kudos
Reply
Loading...
Honored Contributor Honored Contributor
Honored Contributor

Re: Getting an API V1 call into JSON I can use in Javascript?

@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:

  1. Fetch and process labels in the endpoint as you want and let the endpoint return JSON.
  2. Use JS in a custom component to request your endpoint and handle your response ( = the labels) from there on.

 

If I'm wrong, it would be great if you could describe your intention, so I can figure out how to go on. 🙂

0 Kudos
Reply
Loading...
Respected Contributor
Respected Contributor

Re: Getting an API V1 call into JSON I can use in Javascript?

@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"?

0 Kudos
Reply
Loading...