Utilising REST Resource to produce JSON


#1

After successfully creating a JSON data source using squiz page and running JS at server, it is now the bottleneck of the project - taking anywhere up to 5 seconds to compile squiz metadata schemas and print as JSON for use.

If I then take this output, save it in a file, then reference this file instead. The project renders almost instantly.

Now I am looking at taking what I have created in the Squiz page, and making it work using the REST Resource that Squiz offers, and basically using it to produce the JSON output at set time intervals.

I have used the REST resource to complete GET requests to APIs, but never to go and fetch data from inside Squiz itself.

Anyone able to steer me in the right direction?

EXAMPLE:

<script runat="server">
// Request metadata:
var a = %globals_asset_children:123456^as_asset:asset_assetid,asset_data%; 

// Show output:
print(JSON.stringify(a));
</script>

Thought I might get lucky and just plonk this into the ‘process as JS’ section, but it was a pretty big pot shot attempt, and obviously didn’t work.

Thanks again.


#2

Are you able to solve this using matrix cache?

But I was able to get your example output by 1) removing the script tags 2) ensuring Pre-Process Matrix Global Keywords is checked.


#3

Thanks. Pre-Process Matrix Global Keywords makes sense now that you mention it.
Will give this a go!


#4

Worked great. Missing link was the pre-process option.
Thank you.


(Iain Simmons) #5

Also try the ^json_decode modifier before the ^as_asset


#6

Does this offer a speed improvement?
Now that I am requesting data from children in a handful of folders, times are blowing out to almost 10s :persevere:

Record total is still under 100.

If I then take the json output, save it back into Squiz, and then reference this, the page loads in under a second. Obviously not a practical solution as there is a manual element required to refresh the ‘static’ json file once per day :upside_down_face:


(Iain Simmons) #7

You could automate the generation of the static file using triggers and the Set File Contents action. You just need to have an existing file (probably JS file or Text File, but otherwise a generic File asset works too).

You can use a trigger event such as Asset Updated or whatever makes sense for the source of the data.


#8

I haven’t used the Squiz triggers before (except many moons ago in my Squiz training), but I think this might be the solution I need.

Will read through the Triggers section of the Squiz manual to see if it refreshes my memory.

Thanks again.


#9

Realised after coffee this morning that I was approaching the load time issue from the wrong angle.

Instead of creating a trigger in squiz, I can just rewrite my functions to incorporate localStorage.

This should then run the page load using the data from local storage (if it exists, if not go get it), then after running my functions, then go and grab new data from the Squiz REST service which is dishing out the JSON.

Once that has happened, replace what is already in localStorage :sunglasses: