Skip to content

parseValue β€” GTM Variable Template for Value

VARIABLES β€Ί VALUE
parseValue EXTENDED Value
Direct (.tpl) Apply (.tpl)

Converts a string representation back to its original data type (boolean, number, null , etc.). Supports primitive types and optional JSON parsing.



Parse string to boolean
INPUT
String To Parse: true
Enable JSON Parsing: false
OUTPUT
true
Parse string to number
INPUT
String To Parse: 42
Enable JSON Parsing: false
OUTPUT
42

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
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
parseValue()


πŸ“œ 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