toJsonString β GTM Variable Template for String
toJsonString EXTENDED String
Converts the input into a JSON string. If the value cannot be parsed, returns undefined.
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled β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"
Live Sandbox
Section titled βLive Sandboxβ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
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
toJsonString()
Related Variables
Section titled βRelated VariablesβSame category: String
Under the Hood
Section titled β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