parseValue β GTM Variable Template for Value
parseValue EXTENDED Value
Converts a string representation back to its original data type (boolean, number, null , etc.). Supports primitive types and optional JSON parsing.
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled βExamplesβParse string to boolean
INPUT
String To Parse: true
Enable JSON Parsing: false
Enable JSON Parsing: false
OUTPUT
true
Parse string to number
INPUT
String To Parse: 42
Enable JSON Parsing: false
Enable JSON Parsing: false
OUTPUT
42
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.
parseValue
String To Parse
Parses a string value into its native type (boolean, number, etc.).
*** Parse string to boolean***
Input: String To Parse: true
jsn: false
βͺοΈ Output: true
*** Parse string to number***
Input: String To Parse: 42
jsn: false
βͺοΈ Output: 42
*** Parse string to boolean***
Input: String To Parse: true
jsn: false
βͺοΈ Output: true
*** Parse string to number***
Input: String To Parse: 42
jsn: false
βͺοΈ Output: 42
Advanced Setting
βοΈ If enabled, attempts to parse JSON-like strings for complex data types (objects, arrays).
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.
String To Parse 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.
parseValue()
Related Variables
Section titled βRelated VariablesβSame category: Value
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/** * Converts a string representation back to its original data type. * * @param {string} data.src - The string to be converted back to its original value. * @param {boolean} data.jsn - If true, attempts to parse JSON-like strings. * @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 {any} The original value corresponding to the string representation. * * @framework ggLowCodeGTMKit */const makeNumber = require('makeNumber');
const parseValue = function(value, enableJSONParsing) { if (typeof value !== 'string') { return value; } if (value === 'true') return true; if (value === 'false') return false; if (value === 'null') return null; if (value === 'undefined') return undefined; if (value === 'NaN') return 0/0; if (value === 'Infinity') return 1/0; if (value === '-Infinity') return -1/0;
const number = makeNumber(value); if (typeof number === 'number' && number === number) return number;
if (enableJSONParsing) { const JSON = require('JSON'); if (JSON.parse(value) !== undefined) return JSON.parse(value); }
return value;};const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);// ===============================================================================// parseValue - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const value = applyCast(data.pre, data.src);return out(parseValue(value, data.jsn));// ===============================================================================// parseValue(...) β Apply Mode// ===============================================================================/*return function(value, enableJSONParsing) { enableJSONParsing = data.rp1 ? enableJSONParsing : data.jsn; return out(parseValue(value, enableJSONParsing));};*/π§ͺ View Test Scenarios (5 tests)
β
'[example] Parse string to boolean'β
String 'false' - should convert to boolean falseβ
'[example] Parse string to number'β
Non-parseable string - should return unchangedβ
String 'null' - should convert to null