Skip to content

toJsonString — GTM Variable Template for String

VARIABLES › STRING
toJsonString EXTENDED String

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



Examples

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"

GTM Configuration

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


Under the Hood

📜 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