Articles on: Q&A

How to access a specific array elements?

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
{% {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.
Numeric array keys should not be wrapped with quotes.

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: 21/02/2021

Was this article helpful?

Share your feedback

Cancel

Thank you!