How to access a specific array element?
It is possible to use expression language to access specific items in an array if a numeric keys are used. The array can contain strings, number or objects.
Access array at index 1
Access array at index 1 and display value of "item"
Object keys must be wrapped with double quotes, while numeric array keys should not be.
N.B. In expression language, the sub-items are accessed with brackets.
For example, you need to access the value of a location name in your line items meta fields and set the condition to trigger the rule if there is at least one item with the value of location name "abc".
The expression would be:
count(filter({line_items}, 'line_item_metafields["location_name"]["value"] == "abc"')) > 0
Access array at index 1
{% {array_with_strings}[1] %} => "test_2"
Access array at index 1 and display value of "item"
{% {array_with_objects}[1]["item"] %} => "test_2"
Object keys must be wrapped with double quotes, while numeric array keys should not be.
Example JSON
{
"array_with_objects": [
{
"item": "test"
},
{
"item": "test_2"
}
],
"array_with_strings": [
"test",
"test_2"
]
}
Accessing sub-items in the array
N.B. In expression language, the sub-items are accessed with brackets.
For example, you need to access the value of a location name in your line items meta fields and set the condition to trigger the rule if there is at least one item with the value of location name "abc".
The expression would be:
count(filter({line_items}, 'line_item_metafields["location_name"]["value"] == "abc"')) > 0
Updated on: 18/12/2024
Thank you!