pickNested — GTM Variable Template for Object
pickNested EXTENDED Object
Picks properties from a nested object using dot-notation paths.
Examples
Pick nested properties
INPUT
Source Object: {user: {name: "John", age: 30}, status: "active"}
keys: [{value: "user.name"}, {value: "status"}]
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"}]
keys: [{value: "user.profile.address.city"}, {value: "user.role"}]
OUTPUT
Paris
GTM Configuration
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}}
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***
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
🔗 Result Handling — Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
pickNested()
Related Variables
Same category: Object
Under the Hood
📜 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