Flatten JSON Objects Extension
Use the Flatten JSON Objects extension to convert a nested data layer object into a new object with only one layer of key/value pairs.
For more advanced options in converting a data layer, see the Data Layer Converter.
Prerequisites
- See About Extensions
Requirements
- utag v4.38 or later. For more information about updating the
utag.js
template, see our knowledge base article Best Practices for Updating to the Latest Version of utag.js.
How it works
The following are a few examples that demonstrate how the Flatten JSON Objects extension works:
- Objects such as
parent -> child
are flattened asparent.child
. - Array objects such as
parent -> array -> [obj1,objN]
are flattened asparent.array0.obj1key
andparent.arrayN.objNkey
. - For arrays an additional
parent.array.array-size
value are set automatically.
Using the extension
Once the extension is added, the following configuration options are available:
- JSON Object: A JSON object variable that you wish to flatten.
- Output Object: Output object variable to contain the results.
Example
Here is an example of a JSON object including a parent to child relationship and an array:
var person_object = {
"firstName" : "John",
"lastName" : "Doe",
"age" : 25,
"address" : {
"streetAddress" : "1234 Main St",
"city" : "Los Angeles",
"state" : "CA",
"postalCode" : "90010"
},
"phoneNumber" : [
{
"type" : "home",
"number" : "310-555-1234"
},
{
"type" : "fax",
"number" : "310-555-5678"
}
]
};
When flattened, the object becomes:
var person_flatobject = {
"address.city" : "Los Angeles",
"address.postalCode" : "90010"
"address.state" : "CA",
"address.streetAddress": "1234 Main St",
"age" : 25,
"firstName" : "John",
"lastName" : "Doe",
"phoneNumber.array-size" : 2
"phoneNumber0.number" : "310-555-1234"
"phoneNumber0.type" : "home",
"phoneNumber1.number" : "310-555-5678"
"phoneNumber1.type" : "fax"
};
If items from the new object need to be referenced as data layer variables, use the notation as stated in person_flatobject
. For example, access the state
data in the address as person_flatobject.address.state
.
This page was last updated: December 28, 2023