Handlebars mapping commas

Hello,

I'm having trouble with placing commas when I map from one array and depending on the condition it splits the elements into two different arrays. How can I place a comma after each element except the last one in each array? Here is the handlebars mapping and the Resources that I map from:

{
"order_info": {
"shipments": [ {{#each record.GetOrderResponse.OrderShipment}}
{
"items_info": [{{#each ../record.GetOrderResponse.OrderSkuShipment}}{{#compare OrderShipmentId "===" ../ShippmentId}}{{#each ../../record.GetOrderResponse.OrderSku}}
{{#compare OrderSkuId1 "===" ../OrderSkuId2}}
{
"sku": "{{SkuCode}}",
"quantity": "{{Quantity}}"
} {{/compare}}{{/each}}{{/compare}}{{/each}}
],
"ship_method": "{{ShippingSpeed}}",
"carrier": "{{ShippingMethod}}",
"ship_source": "{{ShippingMethod}}",
"ship_date": "{{ShippmentDate}}",
"tracking_number": "{{TrackingNumber}}",
}{{#unless @last}},{{/unless}}
{{/each}}
]}
}
{
"record": {
"GetOrderResponse": {
"OrderSku": [
{
"Quantity": "1",
"Price": "89.0000",
"SkuId": "282828",
"Shiped": "true",
"Discount": "0.0000",
"SkuCode": "1111111XL",
"RegularPrice": "89.0000",
"OrderSkuId1": "11111"
},
{
"Quantity": "1",
"Price": "23.6000",
"SkuId": "292929",
"Shiped": "true",
"Discount": "0.0000",
"SkuCode": "22222222L",
"RegularPrice": "23.6000",
"OrderSkuId1": "22222"
},
{
"Quantity": "1",
"Price": "23.6000",
"SkuId": "272727",
"Shiped": "true",
"Discount": "0.0000",
"SkuCode": "3333333M",
"RegularPrice": "23.6000",
"OrderSkuId1": "33333"
},
{
"Quantity": "1",
"Price": "79.0000",
"SkuId": "242424",
"Shiped": "true",
"Discount": "0.0000",
"SkuCode": "44444444XS",
"RegularPrice": "89.0000",
"OrderSkuId1": "44444"
}
],
"OrderShipment": [
{
"ShippingMethod": "Method1",
"TrackingNumber": "111111111111111111",
"ShippingSpeed": "Standard",
"ShippmentDate": "2020-05-26T00:00:00-05:00",
"ShippmentId": "111111"
},
{
"ShippingMethod": "Method2",
"TrackingNumber": "22222222222222222",
"ShippingSpeed": "Standard",
"ShippmentDate": "2020-04-20T00:00:00-05:00",
"ShippmentId": "222222"
}
],
"OrderSkuShipment": [
{
"OrderSkuId2": "11111",
"OrderShipmentId": "111111"
},
{
"OrderSkuId2": "22222",
"OrderShipmentId": "111111"
},
{
"OrderSkuId2": "33333",
"OrderShipmentId": "222222"
},
{
"OrderSkuId2": "44444",
"OrderShipmentId": "222222"
}

]
}
}
}

Hi, @hristinabendeva615. I got the same error with your code. The following change seemed to work in Dev playground, from:

{{#unless @last}},{{/unless}}

to:

{{#if @last}}{{else}},{{/if}}

Sorry – I can't figure out why the second block worked, but not the first. Let us know if that doesn't solve the problem for you, too.

Hello @stephenbrandt-Stephen-Brandt" rel="nofollow noreferrer">Stephen Brandt,

I have a problem with placing commas after the elements in the items_info list not the shipments list, the JSON I uploaded for the payload had errors in it and it didn't display the problem I have, I updated the JSON and it displays the problem now, also here are some screenshots as well:

Thanks, @hristinabendeva615. Now I'm seeing the same error. Sorry for the hasty reply yesterday.

The reason you can't rely on @last in this case is because of the #compare logic.

I ran a few tests, and the @index and @last values were all correctly maintained within the context of every #each loop.

So, at the inner nesting for "items_info", you will have four iterations: 0, 1, 2, and 3. The @last value will be [true when the index is] 3, given your sample data. However, in only one case in your sample data does the last item match an array item's index in your #compare statement, omitting the comma.

That's an explanation, but not a solution. You'd have to check @last outside of the current #compare blocks, which could get tricky with other @../last and @../../last indices also in play.

There might be something easier and quicker, according to your business rules, like leaving blank "items_info" objects, if that's allowed.