isNotJSON β GTM Variable Template for Logic
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled βExamplesβValid JSON returns false
INPUT
Value To Check: {"key": "value"}
OUTPUT
false
Invalid JSON returns true
INPUT
Value To Check: {key: "value"}
OUTPUT
true
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.
isNotJSON
Value To Check
πΎ The value to check.
Input Setup
Input Function (optional)
βοΈ Optional pre-processing function applied to the input before internal logic (e.g., convert object to string, normalize case). Internal transformations such as case handling will still apply afterward.
Result Handling
Output Function (optional)
βοΈ Optional function to apply to the result before returning it (e.g., str => str + ' β¬', val => val !== undefined for boolean conversion). Useful for chaining transformations on the output.
Value To Check 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.
isNotJSON()
Related Variables
Section titled βRelated VariablesβSame category: Logic
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/** * Checks if a given string is not valid JSON by parsing it and ensuring the result is undefined. * * @param {any} data.src - The value to check. * @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 `src` before checking. * * @returns {boolean} True if the value is not valid JSON, false otherwise. * * @framework ggLowCodeGTMKit */const JSON = require('JSON');const isNotJSON = function(value) { return JSON.parse(value) === undefined;};const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);// ===============================================================================// isNotJSON - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const value = applyCast(data.pre, data.src);return out(isNotJSON(value));// ===============================================================================// isNotJSON() β Apply Mode// ===============================================================================/*return function(value) { return out(isNotJSON(value));};*/π§ͺ View Test Scenarios (4 tests)
β
'[example] Valid JSON returns false'β
'[example] Invalid JSON returns true'β
Object value - should return trueβ
Number value - should return false