Skip to content

parseJSON β€” GTM Variable Template for String

VARIABLES β€Ί STRING
parseJSON EXTENDED String
Direct (.tpl) Apply (.tpl)

Parses a JSON string into a JavaScript value.


Parse JSON object
INPUT
Value to Process: {"name": "John", "age": 30}
OUTPUT
John
Parse JSON array
INPUT
Value to Process: [1, 2, 3, "test"]
OUTPUT
4

This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.

parseJSON
JSON String
πŸ’Ύ The JSON string to parse into an object.
Input Setup
Input Function (optional)
βš™οΈ Optional pre-processing function applied to the JSON string before internal logic (e.g., trim whitespace, replace characters). Internal transformations will still apply afterward.
Result Handling
Output Function (optional)
βš™οΈ Optional function to apply to the result before returning it (e.g., obj => obj.data for nested extraction, obj => Object.keys(obj) for keys only). Useful for chaining transformations on the output.
JSON String string
πŸ’‘ Type any text to see the result update live
🎯 Using special value β€” click input to type instead
Test with:
Falsy
Truthy
parseJSON()


πŸ“œ View Implementation Code
/**
* Parses the provided JSON string into a JavaScript object.
*
* @param {string} data.src - The JSON string that needs to be parsed into an object.
* @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 inp before parsing.
*
* @returns {any|undefined} Returns the JavaScript object or value parsed from the JSON string, undefined if the string is not a valid JSON.
*
* @framework ggLowCodeGTMKit
*/
const JSON = require('JSON');
const parseJSON = function(input) {
if (input == null || input == "undefined" || input == "null" || input === "" || typeof input === "function") return undefined;
return JSON.parse(input);
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// parseJSON - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const processedInput = applyCast(data.pre, data.src);
return out(parseJSON(processedInput));
// ===============================================================================
// parseJSON() – Apply Mode
// ===============================================================================
/*
return function(value) {
return out(parseJSON(value));
};
*/
πŸ§ͺ View Test Scenarios (12 tests)
βœ… '[example] Parse JSON object'
βœ… '[example] Parse JSON array'
βœ… Valid JSON string
βœ… Valid JSON number
βœ… Valid JSON boolean
βœ… Invalid JSON syntax - should return undefined
βœ… Empty string - should return undefined
βœ… Null - should return undefined
βœ… Stringified undefined - should return undefined
βœ… Stringified null - should return undefined
βœ… Function - should return undefined
βœ… Valid number