stringToPath — GTM Variable Template for URL
stringToPath EXTENDED URL
Converts a dot/bracket notation string into a path array.
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"]
GTM Configuration
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
Same category: URL
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