Access value inside nested array using handlebars

Hi,

I have data in the following format. I need to loop thorough the array productData and then array salsify:digital_assets. If the value in "Main Product Image" is eaual to the value in the "salsify:id", I need to fetch the "salsify:url".

{
"id": "19773",
"recordType": "inventoryitem",
"Name": "Test ITEM 1",
"productData": [
{
"salsify:id": "Salsify Test ITEM 1",
"salsify:created_at": "2024-01-23T06:46:20.095Z",
"salsify:updated_at": "2024-01-25T10:53:33.009Z",
"Product SKU": "Test ITEM 1",
"Main Product Image": "234sgfhgfhgjgj4545656768",
"Barcode Type": "UPC",
"salsify:digital_assets": [
{
"salsify:id": "234sgfhgfhgjgj4545656768",
"salsify:url": "http://images.salsify.com/image/upload/s--vbcbv--/123sdsdfdgdfghfh.png",
"salsify:name": "item_Logo",
"salsify:created_at": "2024-01-24T21:47:44.915Z",
"salsify:updated_at": "2024-01-24T21:48:50.565Z",
"salsify:status": "completed",
"salsify:asset_height": 694,
"salsify:asset_width": 2442,
"salsify:asset_resource_type": "image",
"salsify:bytes": 43343,
"salsify:format": "png"
},
{
"salsify:id": "234sgcvcbvnvbnvj41224444",
"salsify:url": "http://images.salsify.com/image/upload/s--vbcbv--/12tertymmvcbb.png",
"salsify:name": "item_Logo",
"salsify:created_at": "2024-01-24T21:47:44.915Z",
"salsify:updated_at": "2024-01-24T21:48:50.565Z",
"salsify:status": "completed",
"salsify:asset_height": 694,
"salsify:asset_width": 2442,
"salsify:asset_resource_type": "image",
"salsify:bytes": 43343,
"salsify:format": "png"
}
]
}
]
}

Given is the handlebars I used.

{{#each productData}}{{#each this.[salsify:digital_assets]}}{{#compare this.salsify:id "===" ../Main Product Image}}{{this.salsify:url}}{{else}}{{/compare}}{{/each}}{{/each}}

But I get an error message as

Message: Could not compile handle bar "{{#each productData}}{{#each this.[salsify:digital_assets]}}{{#compare this.salsify:id "===" ../Main Product Image}}{{this.salsify:url}}{{else}}{{/compare}} {{/each}} {{/each" because "Parse error on line 1:
...pare}} {{/each}} {{/each
-----------------------^
Expecting 'ID', 'STRING', 'NUMBER', 'BOOLEAN', 'UNDEFINED', 'NULL', 'DATA', got 'INVALID'" .Please correct and retry

Can someone suggest the correct expression?

Thanks in advance

Aishwarya

Aishwarya,

I just tested this in the sandbox and was able to get it to successfully evaluate the following expression

{{#each productData}}
{{#each salsify:digital_assets}}
{{#compare salsify:id "===" ../[Main Product Image]}}{{salsify:url}}{{else}}{{/compare}}
{{/each}}
{{/each}}

Please give that a try and let us know if you are still having issues.

@aishwaryamj you just have to make sure to wrap fields in brackets, [], if there are spaces in the field name.

{{#each productData}}{{#each this.[salsify:digital_assets]}}{{#compare this.[salsify:id] "===" ../[Main Product Image]}}{{this.[salsify:url]}}{{else}}{{/compare}}{{/each}}{{/each}}

Hi @kellyizer @tylerlamparter

Thank you so much for the help. My issue is resolved now.