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.
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
GTM Configuration
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
Same category: Value
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 ? data.jsn : enableJSONParsing;
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