solo-io / envoy-gloo

Apache License 2.0
25 stars 14 forks source link

Support for delete json keys in inja template #348

Open nmnellis opened 5 months ago

nmnellis commented 5 months ago

Univision is looking to remove some json keys from the body if certain properties.

Here is the javascript code they currently use to remove it

var content = context.getVariable("request.content")
if ( content !== '' ) { 
    if ( JSON.parse(content).hasOwnProperty('video') ) {
        var payload = JSON.parse(content)
        video = payload['video']; 
        if ( video.hasOwnProperty('mediaId') ) {
            var mediaid = payload['video']['mediaId']+"";
            if ( mediaid.match(/^transmission:matchid:.*/) ) {
                payload['video']['mediaId'] = "video:mcp:unexpected-live-match";
                context.setVariable('request.content', JSON.stringify(payload));
            }
        }
        if ( video.hasOwnProperty('seriesMediaId') ) {
            var seriesmediaid = payload['video']['seriesMediaId'];
            if ( seriesmediaid === "" ) { 
                delete payload['video']['seriesMediaId'];
                if ( video.hasOwnProperty('nextEpisodeMediaId') ) {
                    delete payload['video']['nextEpisodeMediaId'];
                }   
                context.setVariable('request.content', JSON.stringify(payload))
            }
        } else if ( video.hasOwnProperty('nextEpisodeMediaId') ) {
            delete payload['video']['nextEpisodeMediaId'];
            context.setVariable('request.content', JSON.stringify(payload))
        } 
    } else {
        context.setVariable('vixdebug.remove_empty_series_media_id','payload does not have a json video object')
    }
}

PR: https://github.com/solo-io/envoy-gloo/pull/347