How to create a data-element based on a JSON object array? | Community
Skip to main content
Level 2
August 11, 2016
Solved

How to create a data-element based on a JSON object array?

  • August 11, 2016
  • 1 reply
  • 1129 views

 Hello all,

I'm working with the W3 dataLayer and want to trigger a rule when a certain product is on the page, but I don't know how I can create a data-element that is based on a array. So for example I have the following dataLayer and I want to trigger a rule when the product name contains laptop. How can I do this?

var digitalData = {
    "listing":
        [
        {
            "productInfo":
            {
            "productName": "tv", 
            "productId": "19381654"
            }
            ,
            "productInfo":
            {
            "productName": "laptop", 
            "productId": "19381652"
            }
            
        }
        ]
    }

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by ParitMittal

Hi Guidov, 

There is a particular way in which we can represent path as an array in a JS object type for Data Elements. Please see the following content for more details.

By default in JavaScript if you want to extract userName value then you would use simple javascript syntax.
In DTM it is different. If you want to create a data element to extract productName value from a complex Json object using Array then you will have to use-
Example of rule : 
 for this Path- digitalData.listing[0].productInfo.productName
you have to use a path like this - 
digitalData.listing.0.productInfo.productName
 
So to be able to extract value from an array in JavaScript you would use arrayName[x] where x is the index of the value in the array.
In DTM when you use a data element based on JS path then you will need to use the following if you want to extract a value from a JavaScrtipt array : arrayName.x where x is the index of the value in the array.
 
Thanks & Regards
Parit Mittal

1 reply

ParitMittal
ParitMittalAccepted solution
Level 10
August 12, 2016

Hi Guidov, 

There is a particular way in which we can represent path as an array in a JS object type for Data Elements. Please see the following content for more details.

By default in JavaScript if you want to extract userName value then you would use simple javascript syntax.
In DTM it is different. If you want to create a data element to extract productName value from a complex Json object using Array then you will have to use-
Example of rule : 
 for this Path- digitalData.listing[0].productInfo.productName
you have to use a path like this - 
digitalData.listing.0.productInfo.productName
 
So to be able to extract value from an array in JavaScript you would use arrayName[x] where x is the index of the value in the array.
In DTM when you use a data element based on JS path then you will need to use the following if you want to extract a value from a JavaScrtipt array : arrayName.x where x is the index of the value in the array.
 
Thanks & Regards
Parit Mittal