Simple iRulesLX JSON rewrite

Problem this snippet solves:

Like rewriting the Location field in a redirect, it's sometimes required to rewrite JSON data, returned in an HTTP response. Whilst it would be possible to write using traditional iRules, the task is made more simple (and less risky) by using iRulesLX.

In the following example, an HTTP response contains JSON data with fields containing external URLs. These need to be rewritten to an internal URL for the purpose of internal routing.


The concept can be used to rewrite any JSON data, however more complicated JSON containing arrays for example would need to be taken into consideration.

How to use this snippet:

use the following iRule to call iRulesLX and pass the necessary parameters

    set newHost ""
    set jsonKeys "firstUrl secondUrl"
    set rpcHandle [ILX::init "json-parse-plugin" "json-parse-extension"]

    if {[HTTP::header "Content-Type"] eq "application/json"} {
        HTTP::collect [HTTP::header "Content-Length"]

    set payload [HTTP::payload]
    set result [ILX::call $rpcHandle "setInternalUrl" $payload $jsonKeys $newHost]
    HTTP::payload replace 0 [HTTP::header "Content-Length"] $result

When used in combination with the iRulesLX code below the host portion of the URIs in the JSON data are rewritten and sent back to the origin by replacing the HTTP payload


Code :

const f5 = require('f5-nodejs');
const url = require('url');
const ilx = new f5.ILXServer();

function setInternalUrl(req, res) {

    var json = JSON.parse(req.params()[0]);
    var jsonObj = req.params()[1].split(' ');
    var newHost = req.params()[2];

    for (var i = 0; i < jsonObj.length; i++) {
        if (typeof json[jsonObj[i]] == "string") {
            var oldUrl = url.parse(json[jsonObj[i]]);
   = newHost;
            var newUrl = decodeURI(url.format(oldUrl));
            json[jsonObj[i]] = newUrl;
        } else {
            json = {"error":"unable to rewrite"};

ilx.addMethod('setInternalUrl', setInternalUrl);

Tested this on version:

Updated Jun 06, 2023
Version 2.0

Was this article helpful?

1 Comment

  • Great bit of code and excatly what I'm looking for, but as the response is gzip encoded the irule fails as there is no content length header! is there any other way of capturing the payload without content-length ?