How to access array inside an object (Event Data Variable)

How can I access 2nd level nested objects using the Event Data Variable?

I have the following JSON in my Purchase body:

  "transaction_info": {
    "transaction_id": "1716282385757_17024627607901",
    "total_amount": 50.00,
    "currency": "USD",
    "total_items": 1,
    "items": [
      {
        "item_id": "SKU123",
        "name": "Widget",
        "category": "Gadgets",
        "price": 50.00,
        "quantity": 1
      }
    ]
  },

I want to access for example transaction_info > items > item_id. I have created this variable:

But it is showing as “undefined” when inspecting the Variables in the GTM debugger.

Do I need to use this Variable Template? GitHub - stape-io/object-property-extractor-variable: Object Property Extractor Variable for Google Tag Manager Server Side

I managed to make it work, if someone is interested. The thing is that the items key is an array with a list of objects.

So the way to access it is the following (accessing to the first position of the array): transaction_info.items.0.item_id

You can def get that but targeting the item position within an array.

but, if this is a transaction (assuming from the transaction) or a list, its likely that you will need to process each item within the array, as this would only ever give you the first item (0) within the array even if someone has purchased multiple items. same goes for item_lists.

ie:

For the Facebook Tag, I have it like this:

Not sure if there is a more optimal way to do it.

The problem is that Facebook expects some items inside the array and some items outside the array. And in my webhook I have every item specific field inside the array.

And as @Chris_Bradley mentioned, this is only viable for 1 item inside the array. Not sure how to do it with multiple items.

@hustleou

I just created a custom template, then JS coded an array based on what I wanted the output and then made a variable from that template.

Makes sense! But @Dan @Alex I wonder if there is a better way to do this with some already developed template?

The easiest way would to follow GA4 schemas in your webhook payloads, for instance if you have items array in your Event Data (structured and with keys as per GA4 specs https://developers.google.com/analytics/devguides/collection/ga4/reference/events?client_type=gtm) than the FB tag will parse that automatically and break it down into contents, content_ids etc.

We have a variable (Tag Manager Template Gallery) that can convert any custom array into datapoints for most popular destionations, but that one is on the web currently, I do have it listed to be ported to the server but I can’t promise any timelines

Found this Variable Server Template that does the mapping correctly: GitHub - Sogody/array-map-gtm-template