Skip to content

toJsonString β€” GTM Variable Template for String

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

Converts the input into a JSON string. If the value cannot be parsed, returns undefined.



Object to JSON
INPUT
Input Value: {name: "John", age: 30}
OUTPUT
{"name":"John","age":30}
Array to JSON
INPUT
Input Value: [1, 2, 3, "test"]
OUTPUT
[1,2,3,"test"]
String to JSON
INPUT
Input Value: hello
OUTPUT
"hello"

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

toJsonString
Input Value
πŸ’Ύ The JavaScript object or value to convert to JSON.
Input Setup
Input Function (optional)
βš™οΈ Optional pre-processing function applied to the input before internal logic (e.g., clean object properties, add metadata). Internal transformations will still apply afterward.
Result Handling
Output Function (optional)
βš™οΈ Optional function to apply to the result before returning it (e.g., str => str.replace(/"/g, "'") for quote replacement, str => btoa(str) for base64 encoding). Useful for chaining transformations on the output.
Input Value object
πŸ’‘ Type any text to see the result update live
🎯 Using special value β€” click input to type instead
Test with:
Falsy
Truthy
toJsonString()


πŸ“œ View Implementation Code
/**
* Converts a JavaScript object or value into a JSON string.
*
* @param {any} data.inp - The JavaScript object or value to be converted into a JSON string.
* @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 conversion.
*
* @returns {string|undefined} A JSON string representation of the input value. Returns undefined if the value cannot be parsed (e.g. the object has a cycle).
*
* @framework ggLowCodeGTMKit
*/
const JSON = require('JSON');
const toJsonString = function(input) {
return JSON.stringify(input);
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// toJsonString - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const processedInput = applyCast(data.pre, data.inp);
return out(toJsonString(processedInput));
// ===============================================================================
// toJsonString() – Apply Mode
// ===============================================================================
/*
return function(value) {
return out(toJsonString(value));
};
*/
πŸ§ͺ View Test Scenarios (7 tests)
βœ… '[example] Object to JSON'
βœ… '[example] Array to JSON'
βœ… '[example] String to JSON'
βœ… Number value - converts to JSON string
βœ… Nested object - converts complex structure to JSON string
βœ… Object with function property - function is omitted from JSON
βœ… Undefined as standalone value - returns undefined