stringToPath β GTM Variable Template for URL
stringToPath EXTENDED URL
Converts a dot/bracket notation string into a path array.
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled βExamplesβDot notation to path
INPUT
Path to Property: user.name.first
OUTPUT
["user", "name", "first"]
Bracket notation with index
INPUT
Path to Property: items[0]
OUTPUT
["items", "0"]
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.
stringToPath
Path to Property
πΎ Property path using dot and bracket notation.
Input Setup
Input Function (optional)
βοΈ Optional pre-processing function applied to the input before internal logic (e.g., convert object to string, normalize case). Internal transformations such as case handling will still apply afterward.
Result Handling
Output Function (optional)
βοΈ Optional function to apply to the result before returning it (e.g., str => str + ' β¬', val => val !== undefined for boolean conversion). Useful for chaining transformations on the output.
Path to Property string
π‘ Type any text to see the result update live
π― Using special value β click input to type instead
Test with:
Falsy
Truthy
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
stringToPath()
Related Variables
Section titled βRelated VariablesβSame category: URL
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/** * Converts a string to a property path array. * * @param {string} data.src - The string to convert to a property path array. * @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 conversion. * * @returns {Array} Returns the property path array, or empty array if input is invalid. * @author Lodash (https://lodash.com) - Original regex and parsing approach * @see https://github.com/lodash/lodash/blob/master/stringToPath.js * @modified by GwenG β Rewrote the full function to adapt to the ggLowCodeGTMKit framework * * @framework ggLowCodeGTMKit */const getType = require('getType');
const stringToPath = function(path) { if (getType(path) === 'array') return path; if (typeof path !== 'string') return []; if (path === '') return []; if (path.indexOf('[') === -1 && path.indexOf('\\') === -1) return path.split('.');
const pathSegmentRegex = "[^.[\\]]+|\\[(?:(-?\\d+(?:\\.\\d+)?)|([\"'])((?:(?!\\2)[^\\\\]|\\\\.)*?)\\2)\\]"; const escapedCharRegex = '\\\\(.)'; let pathSegments = []; let unparsed = path;
while (unparsed.charAt(0) === '.') { pathSegments.push(''); unparsed = unparsed.slice(1); }
while (unparsed.length > 0) { const segmentMatch = unparsed.match(pathSegmentRegex); if (!segmentMatch) { break; } const matchedSegment = segmentMatch[0]; const numberIndex = segmentMatch[1]; const quoteType = segmentMatch[2]; const quotedKey = segmentMatch[3];
if (quoteType && quotedKey) { let escapedQuotedKey = quotedKey; let unescapedKey = ''; while (escapedQuotedKey.length > 0) { const escapeMatch = escapedQuotedKey.match(escapedCharRegex); if (!escapeMatch) { unescapedKey += escapedQuotedKey; break; } unescapedKey += escapedQuotedKey.substring(0, escapeMatch.index); unescapedKey += escapeMatch[1]; escapedQuotedKey = escapedQuotedKey.substring(escapeMatch.index + escapeMatch[0].length); } pathSegments.push(unescapedKey); } else if (numberIndex) { const stringifiedIndex = numberIndex.toString(); pathSegments.push(stringifiedIndex); } else if (matchedSegment) { pathSegments.push(matchedSegment); } unparsed = unparsed.substring(segmentMatch.index + matchedSegment.length); }
let trailingDots = 0; let dotPosition = path.length - 1; while (dotPosition >= 0 && path.charAt(dotPosition) === '.') { trailingDots++; dotPosition--; } for (let dotIndex = 0; dotIndex < trailingDots; dotIndex++) { pathSegments.push(''); } return pathSegments;};
const sπ§ͺ View Test Scenarios (23 tests)
β
'[example] Dot notation to path'β
'[example] Bracket notation with index'β
Mixed dot and bracket notation with numbersβ
Bracket notation with quoted strings containing dotsβ
Path with leading dotβ
Multiple consecutive bracket notationsβ
Path with consecutive dots (skips empty parts)β
Path with unclosed bracketβ
Complex path with multiple notation typesβ
Path with NaN inside bracketsβ
Empty brackets between segmentsβ
Extremely complex mixed pathβ
Multiple leading dotsβ
Escaped quotes in bracket notationβ
Brackets inside quotesβ
Floating point numbers in bracketsβ
Negative numbers in bracketsβ
Mixed quote typesβ
Just a string with no dots or bracketsβ
Empty stringβ
String with just a dotβ
Trailing dotsβ
Invalid input return empty array