Khoros Atlas Logo

Respected Contributor
Respected Contributor

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

Jump to solution

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. 

Tags (2)
0 Kudos
20 Replies 20
Highlighted
Honored Contributor Honored Contributor
Honored Contributor

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

Jump to solution

@IanAtJMP  - You could use the restapi.response_format parameter to return JSON:

 

restapi.response_format=json

 

For more details see developer docs.

 

Alternatively, you can move your API requests to an endpoint and build a custom JSON response for that endpoint. Afterwards you can request your endpoint from your JS code.

Reply
Loading...
Respected Contributor
Respected Contributor

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

Jump to solution

@cike Thanks, however, I'm still unable to feed that directly into JS. To wit:

 

<#-- EX: https://community-stage.jmp.com/restapi/v1/categories/id/discovery/labels -->
<#assign someLabels = restadmind("/${context}/id/${contextId}/labels?restapi.response_format=json") />

<script>
console.log('script');
let labels = [${someLabels}];

console.log('labels',labels);
</script>

Gets me a freemarker error => 


Screen Shot 2019-07-09 at 9.09.27 AM.png

 

How would creating an endpoint for this make that result any different?

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

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

Jump to solution

@IanAtJMP  - I think you have two errors in your REST call:

 

Firstly, you need to check your APIv1 request. I think you need to add an additional "/labels" path parameter to get a list of labels for your category:

 

<#-- APIv1 request: <a href="https://community-stage.jmp.com/restapi/vc/categories/id/[id]/labels/labels" target="_blank">https://community-stage.jmp.com/restapi/vc/categories/id/[id]/labels/labels</a> -->

<#assign someLabels = restadmind("/${context}/id/${contextId}/labels/labels?restapi.response_format=json") />

 

Take a look into the developers docs and APIv1 reference.

 

Secondly, to access the labels from the API response you need to access the labels object of the response. Your request will return something similar the example below:

 

{
  "response": {
    "status": "success",
    "labels": {
      "label": [
        {
          "type": "label",
          "href": "/labels/id/1985",
          "text": {
            "type": "string",
            "$": "Festnetz | Internet | Fernsehen"
          },
          "id": {
            "type": "int",
            "$": 1985
          }
        },
        {
          "type": "label",
          "href": "/labels/id/1982",
          "text": {
            "type": "string",
            "$": "Sonstiges"
          },
          "id": {
            "type": "int",
            "$": 1982
          }
        },
      }
    ]
  }
}

 

 

The have access to the label values, you have to access your Freemarker variable like this:

 

<#assign labels = someLabels.labels.label />

 

 

Be aware to add checks for empty responses or lists. Otherwise you will get Freemarker errors for accessing non-existing objects.

Reply
Loading...
Respected Contributor
Respected Contributor

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

Jump to solution

Will try that out, thanks. Then I can assign it to a JS variable like so?

var labels = [${labels}]

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

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

Jump to solution

@IanAtJMP  - You'll need to wrap your Freemarker variable in "", because Javascript can't handle ${labels} and will run into errors.

While the Freemarker JSON response should be valid you could parse the response with JSON.parse:

var labels = JSON.parse('${labels}')

 

Reply
Loading...
Respected Contributor
Respected Contributor

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

Jump to solution

Thanks, but I can't seem to get this to work no matter what I do:

 

Here's the JSON output from the call directly from the browser: 

<a href="https://community-stage.jmp.com/restapi/v1/categories/id/discovery/labels/labels?restapi.response_format=json" target="_blank">https://community-stage.jmp.com/restapi/v1/categories/id/discovery/labels/labels?restapi.response_format=json</a> 

OUTPUT: 

{
  "response": {
    "status": "success",
    "labels": {
      "label": [
        {
          "type": "label",
          "href": "\/labels\/id\/55",
          "id": {
            "type": "int",
            "$": 55
          },
          "text": {
            "type": "string",
            "$": "Data Access and Manipulation"
          }
        },
        {
          "type": "label",
          "href": "\/labels\/id\/28",
          "id": {
            "type": "int",
            "$": 28
          },
          "text": {
            "type": "string",
            "$": "Data Exploration"
          }
        }
      ]
    }
  }
}

 

RELEVANT ENDPOINT CODE:

<#assign contextId = 'discovery'/>
<#assign context = 'categories'/>

<#-- MAIN LABELS -->
<#-- EX: <a href="https://community-stage.jmp.com/restapi/v1/categories/id/discovery/labels" target="_blank">https://community-stage.jmp.com/restapi/v1/categories/id/discovery/labels</a> -->
<#assign someLabels = restadmin("/${context}/id/${contextId}/labels/labels?restapi.response_format=json") />

var labels = JSON.parse('${someLabels}');
console.log('labels',labels);

 

Output, same Screen Shot 2019-07-10 at 11.03.01 AM.pngFreemarker Error: 

 

 

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

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

Jump to solution

The error occurs, because you try to access the complete response object. But this is not supported.

The first element of the response you're able to access is the label array:

...
label: [
 ...
 ...
]

 

To solve the error, just add .labels.label to your code:

var labels = JSON.parse('${someLabels.labels.label}');

 

The keep your Javascript code as clean as possible, you could also add the .labels.label part to your Rest request:

<#assign someLabels = restadmin("/${context}/id/${contextId}/labels/labels?restapi.response_format=json").labels.label />

 

Reply
Loading...
Respected Contributor
Respected Contributor

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

Jump to solution

Sadly, this still throws the same error.

It would seem it should be easier than this to get something that's supposedly returned in JSON to initialize a JS variable with it...

<#assign contextId = 'discovery'/>
<#assign context = 'categories'/>

<#-- MAIN LABELS -->
<#-- EX: <a href="https://community-stage.jmp.com/restapi/v1/categories/id/discovery/labels" target="_blank">https://community-stage.jmp.com/restapi/v1/categories/id/discovery/labels</a> -->
<#assign someLabels = restadmin("/categories/id/discovery/labels/labels?restapi.response_format=json").labels.label />
  

  var labels = JSON.parse('${someLabels.labels.label}');
  console.log('labels',labels);
  

Screen Shot 2019-07-11 at 9.17.55 AM.png

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

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

Jump to solution

You must not add the .labels.label part in both locations. Just one is needed. I recommend the following:

<#assign contextId = 'discovery'/>
<#assign context = 'categories'/>

<#-- MAIN LABELS -->
<#-- EX: <a href="https://community-stage.jmp.com/restapi/v1/categories/id/discovery/labels" target="_blank">https://community-stage.jmp.com/restapi/v1/categories/id/discovery/labels</a> -->
<#assign someLabels = restadmin("/categories/id/discovery/labels/labels?restapi.response_format=json").labels.label />
  

  var labels = JSON.parse('${someLabels}');
  console.log('labels',labels);

 

Additionally, don't forget to wrap your Javascript in a <script> tag or Khoros' <liaAddScript> directive.

Reply
Loading...