Iterate json aray


(N Wallis) #1

Hi there, 

 

I am successfully getting JSON data from a REST resource and rendering this data using an asset listing.  All is working nicely.  

 

Originally one of my JSON properties was a string which was easily displayed in the asset listing using the correct keyword.   However, requirements for the task I am working on have changed this data type to an Array and I am wondering if there is anyway that I can iterate this array using Matrix keywords so that I can output the string.  I haven't had any luck so far in finding a keyword.

 

Any ideas?

 

Thanks,

Nathan


(Nick Papadatos) #2

Have you tried using a REST Resource Javascript asset instead?? https://manuals.matrix.squizsuite.net/web-services/chapters/rest-resource-assets#REST-Resource-JavaScript

 

If you have a look at the manual it shows you how to iterate through the different buckets of content


(Bart Banda) #3

There are plenty of array keyword modifiers that you can try from https://manuals.matrix.squizsuite.net/keyword-replacements/chapters/keyword-modifiers

Most of them added in 5.1.

 

But yes, if you use a REST JS asset as Nicky said you can traverse the data using server side JS and print it in anyway you want which might be easier. 


(N Wallis) #4

Yep, thanks for the input. 

 

I am now using the Javascript rest resource and it has help alot in massaging the data how I need it for Matrix.  But it still doesn't really solve the iterating the JSON array.  I want to be able to iterate the array in the asset listing.  Currently the only way I can see to do this is by iterating the array using the Javascript REST resource and generating the HTML there as single string and then injecting this into the asset listing (which avoids trying to iterate the JSON array using asset listing keywords). 

 

Maybe I am not using the Javascript REST Resource correctly.   My technique goes like this:

 

JSON.parse(_REST.response.body);

.

.

Manipulate the data

.

.

print(JSON.stringify(event));

 

Does that look right?


(Bart Banda) #5

That looks alright to me, but you could potentially skip the asset listing at the end and just print the list via the JS output using a for loop?