Skip to content

isJSON — GTM Variable Template for Condition

VARIABLES › CONDITION
isJSON EXTENDED Condition

Checks if a given input is valid JSON.



Examples

Valid JSON returns true
INPUT
Value To Check: {"key": "value"}
OUTPUT
true
Invalid JSON returns false
INPUT
Value To Check: {key: "value"}
OUTPUT
false

GTM Configuration

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

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


Under the Hood

📜 View Implementation Code
/**
 * Checks if a given string is valid JSON by parsing it and ensuring the result is not 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 valid JSON, false otherwise.
 *
 * @framework ggLowCodeGTMKit
 */
const JSON = require('JSON');
const isJSON = function(value) {
	return JSON.parse(value) !== undefined;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// isJSON - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(isJSON(value));
// ===============================================================================
// isJSON() – Apply Mode
// ===============================================================================
/*
return function(value) {
   return out(isJSON(value));
};
*/
🧪 View Test Scenarios (4 tests)
✅ '[example] Valid JSON returns true'
✅ '[example] Invalid JSON returns false'
✅ Object value - should return false
✅ Number value - should return true