I’m trying to automate some sanity checks for my GTM account to see that every event/object stays the same after website releases. Therefore I want to create/use some kind of JSON validator in javascript that can be used directly in the browser.
Is there any way to validate a JSON object in the browser? I’m working on a solution to scrape the dataLayer objects directly in the browser then check if the objects contain the right data. I have found a lot of different “JSON validators” in GitHub and such but none that supports to check the JSON objects in the console in browser.
For now I don’t think it’s needed to check whole JSON schema instead every object separately.
I have written some code to check
var dataLayerList = dataLayer;
var dataLayerEventName;
for(var i = 0; i < datalayerList.length; i++){
var dataLayerObj = datalayerList[i];
if(dataLayerObj.event){
dataLayerEventName = dataLayerObj.event;
switch(dataLayerEventName){
case 'foo':
foo(dataLayerObj);
break;
case 'bar':
bar(dataLayerObj);
break;
}
}
}
function foo(dataLayerObj){
//check if dataLayerObj contains the same keys and value types as predefined JSON object
}
function bar(dataLayerObj){
//check if dataLayerObj contains the same keys and value types as predefined JSON object
}
What I want to do is to see if the object that is passed in the parameters of each function follows the predefined JSON objects.