pickNested β GTM Variable Template for Object
pickNested EXTENDED Object
Picks properties from a nested object using dot-notation paths.
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled β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
Live Sandbox
Section titled βLive Sandboxβ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
Section titled βRelated VariablesβSame category: Object
Under the Hood
Section titled β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