Skip to content

parseJsonString — GTM Variable Template for Object

VARIABLES › OBJECT
parseJsonString EXTENDED Object

Parses a JSON string into a JavaScript value.



Examples

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

GTM Configuration

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

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


Under the Hood

📜 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 parseJsonString = 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);

// ===============================================================================
// parseJsonString - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const processedInput = applyCast(data.pre, data.src);
return out(parseJsonString(processedInput));

// ===============================================================================
// parseJsonString() – Apply Mode
// ===============================================================================
/*
return function(value) {
   return out(parseJsonString(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