New key values being added to JSON object using <amp-script> are being sanitized and removed

121 Views Asked by At

I am trying to add 3 new key pair values to a JSON object defined in tag using as need to add custom javascript to calculate those values but these values are being sanitized and removed in the response (getting the warning in console). I have first JSON.parse the JSON object, updated values, and then set the JSON using setAttribute(). The JSON object here is user defined in adAttributes.

    <amp-ad class="ad-rest-of-world" {{ adAttributes }}></amp-ad>
</amp-script>

<script id="targeting-key-values" type="text/plain" target="amp-script">
    var tag = document.getElementsByClassName('ad-rest-of-world')[0];
    var jsonData = JSON.parse(tag.getAttribute('json'));
    if ('object' !== typeof jsonData['targeting']) {
        jsonData['targeting'] = Object.create({});
    }
    //added 3 new key-value pairs
    jsonData['targeting']['recency'] = recency;
    jsonData['targeting']['hour'] = hour;
    sessionStorage.setItem('upv', upv);
    jsonData['targeting']['upv'] = upv;
    tag.setAttribute('json', JSON.stringify(jsonData));
</script>```

I expected that the 3 values are being added to json but these are being added, sanitized and removed. How to solve the issue?
0

There are 0 best solutions below