This will only returns the folder name because it’s the direct children yes? var assets = %asset_children_link_type_1^as_asset:asset_short_name,asset_url^empty:[]%;
returns
2023 - 0
2022 - 1 etc
let html = '';
assets.forEach((asset, index) => {
if(asset.asset_type_code === 'folder'){
html +=`<p>${asset.asset_name} - ${index}</p>`;
}
})
print(html)
let assets = %asset_children_link_type_1^as_asset:asset_name,asset_type_code^empty:[]%; Gives me the folder names based on the above
Now I need to get to its children iside the folder let dataRecords = %I need to chain the above asset_children to get to the childrens children%
What I have that’s not working
let dataRecods = %asset_children_link_type_1^as_asset:asset_children_link_type_2^empty:[]%;
Once you have the asset ID’s of the children, you can do a forEach loop to return the values you need for the children.
Once you get down to the grandchildren level, it starts to get messy.
I find that it’s easier at this point to just use an asset listing and a REST resource so its cached (asset listing does not have to run every time) to give you the output you want (JSON payload) and use SSJS from there.
What ended up working me was: %asset_children_link_type_1^json_decode^as_asset:{asset_children_link_type_2^json_decode^as_asset:asset_data}^empty:[]%
You can also then pick these attributes …^as_asset:asset_url,asset_name,asset_url etc
What wasn’t clear to me initially was that this keyword modifier creates a mulitidimensional array, you then need to create a nested for loop to access each column and row in the array.
9 out of 10 times I would use an asset lister but this was more of a discovery process for me but someone else may benefit from this post.
What I like to do when working with SSJS and matrix asset attributes is to use client side script to output the data so I can see what is available and also the structure of data.
Example:
<script>
let log = console.log;
let output = '';
let directChildren = %asset_children_link_type_1^as_asset:asset_name,asset_type_code^empty:[]%;
let subPages = %asset_children_link_type_1^json_decode^as_asset:{asset_children_link_type_2^json_decode^as_asset:asset_data}^empty:[]%;
let data = %asset_children_link_type_1^as_asset:asset_children_link_type_2^empty:[]%;
log('directChildren: ', directChildren);
log('subPages: ', subPages);
log('data: ', data);
</script>
Use the inspector to review the outputs.
The only way to print the subleves is to nest your for each loop. Please note the subPages Array is now a multidimensional array, read more