Skip to content

pickNested β€” GTM Variable Template for Object

VARIABLES β€Ί OBJECT
pickNested EXTENDED Object
Direct (.tpl) Apply (.tpl)

Picks properties from a nested object using dot-notation paths.



Pick nested properties
INPUT
Source Object: {user: {name: "John", age: 30}, status: "active"}
keys: [{value: "user.name"}, {value: "status"}]
OUTPUT
John
Deep nested pick
INPUT
Source Object: {user: {profile: {address: {city: "Paris", zip: "75001"}}, role: "admin"}}
keys: [{value: "user.profile.address.city"}, {value: "user.role"}]
OUTPUT
Paris

This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.

pickNested
Source Object
πŸ’Ύ The source object to pick nested properties from.

Supported formats:
  βœ“ Object variable: {{myObject}}
  βœ“ Object literal: {user: {name: "John", age: 30}}
Property Paths to Pick
πŸ’Ύ Array of nested property paths to pick from the source object. Use dot notation for nested properties (e.g., "user.profile.name").

Supported formats:
  βœ“ Nested path: "user.name", "settings.theme.color"
  βœ“ Top-level: "status"

*** Pick nested properties***

*** Deep nested pick***
βŠ–
Input Setup
Input Function (optional)
βš™οΈ Optional pre-processing function applied to the object before checking (e.g., normalize object structure, parse JSON).
Result Handling
Output Function (optional)
βš™οΈ Optional function to apply to the result object before returning it (e.g., obj => JSON.stringify(obj), obj => Object.keys(obj).length). Useful for chaining transformations on the output.
Source Object object
πŸ’‘ Type any text to see the result update live
🎯 Using special value β€” click input to type instead
Test with:
Falsy
Truthy
Property Paths to Pick list
pickNested()


πŸ“œ View Implementation Code
/**
* Creates a new object composed of selected nested properties using paths.
*
* @param {Object} data.src - The source object.
* @param {Array} data.keys - Array of objects with path strings to pick (e.g., "user.profile.name").
* @param {Function|string} [data.out] - Optional output handler: function to transform result or string with format.
*
* Direct-mode specific parameters:
* @param {Function} [data.pre] - Optional pre-processor function to transform src before processing.
*
* @returns {Object} A new object containing only the specified nested properties.
*
* @framework ggLowCodeGTMKit
*/
const getType = require('getType');
const createFlatArrayFromValues = function(list, property) {
const result = [];
if (!list) return result;
for (let i = 0; i < list.length; i++) {
const val = list[i][property];
if (getType(val) === 'array') {
for (let j = 0; j < val.length; j++) {
result.push(val[j]);
}
} else if (val) {
result.push(val);
}
}
return result;
};
const pickNested = function(object, paths) {
const result = {};
if (object == null || typeof object !== 'object') return result;
for (let i = 0; i < paths.length; i++) {
const path = paths[i];
const segments = path.split('.');
let src = object;
let valid = true;
for (let j = 0; j < segments.length - 1; j++) {
const key = segments[j];
if (src == null || typeof src !== 'object' || !src.hasOwnProperty(key)) {
valid = false;
break;
}
src = src[key];
}
if (valid) {
const lastKey = segments[segments.length - 1];
if (src != null && typeof src === 'object' && src.hasOwnProperty(lastKey)) {
// Only now write to result
let dst = result;
for (let j = 0; j < segments.length - 1; j++) {
const key = segments[j];
if (dst[key] === undefined || typeof dst[key] !== 'object') {
dst[key] = {};
}
dst = dst[key];
}
dst[lastKey] = src[lastKey];
}
}
}
return result;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// pickNested - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
const paths = createFlatArrayFromValues(data.keys, "value");
return out(pickNested(value, paths));
// ===============================================================================
// pickNested(...) – Apply Mode
// ===================================
πŸ§ͺ View Test Scenarios (5 tests)
βœ… '[example] Pick nested properties'
βœ… '[example] Deep nested pick'
βœ… Handle non-existent paths returns empty or partial object
βœ… Pick multiple properties at same nesting level
βœ… Invalid input returns empty object