Back

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.

In this article:

Table of Contents Placeholder

Prerequisites

Requirements

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 as parent.child.
  • Array objects such as parent -> array -> [obj1,objN] are flattened as parent.array0.obj1key and parent.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 -> child relationship and an array:

example.png

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

flatobject_datasource.png