Replace arrays or objects

The bulk patches discussed above change simple scalar string, numeric, or boolean values, but you can also use JSON Patch to replace more complex object and array data structures. This advanced technique offers you great power and flexibility when modifying all your rule tree configurations.

Suppose you specify a NetStorage origin to offload multimedia content, and you want to bulk-change the storage location and the CP code to track the traffic. Unlike most other PAPI behavior options, the origin behavior's netStorage option specifies a complex nested object value:

{
    "name" : "origin",
    "options" : {
        "originType" : "NET_STORAGE",
        "httpsPort" : 443,
        "netStorage" : {
            "cpCode" : 12345,
            "downloadDomainName" : "legacy.download.example.akamai.com",
            "name" : "Legacy Storage Area"
        }
    }
}

In this case, you could run three different bulk searches for each member of the netStorage object, and replace each in separate batches, but that would be very inefficient:

$..behaviors[?(@.name == 'origin'].options.netStorage.name
$..behaviors[?(@.name == 'origin'].options.netStorage.cpCode
$..behaviors[?(@.name == 'origin'].options.netStorage.downloadDomainName

You could also reconfigure a single set of bulk patch operations to replace all three cpCode, downloadDomainName, and name values separately.

As a much easier alternative, simply match the entire netStorage object:

$..behaviors[?(@.name == 'origin'].options.netStorage

Specify a replacement value that contains the entire object for the alternate NetStorage origin. This patch performs all the necessary changes at once:

"patches": [
    {
        "op": "replace",
        "path": "/rules/behaviors/0/options/netStorage",
        "value": {
            "cpCode": 54321,
            "downloadDomainName": "new.download.example.akamai.com",
            "name": "Alternate Storage Area"
        }
    }
]

You can use the same technique to replace arrays. As a simple example, suppose you're already using a fileExtension criteria to respond to .jpg and .gif file extensions, and you want to add .png to the set. The criteria specifies an array of values:

"criteria": [
    {
        "name": "fileExtension",
        "options": {
            "matchCaseSensitive": true,
            "matchOperator": "IS_ONE_OF",
            "values": [
                "jpg",
                "gif"
            ]
        }
    }
]

As detailed in the next section, you can add new values to the array. But you can also replace the entire array with the full set of values you want.

This search matches the values of any fileExtension criteria that already specifies jpg as a value:

$..criteria[?(@.name == 'fileExtension'].options[?('jpg' in @.values)].values

You can follow up by patching in as a replacement value the entire array of file extensions you want to match:

"patches": [
    {
        "op": "replace",
        "path": "/rules/children/0/criteria/1/options/values",
        "value": [
            "png",
            "jpg",
            "gif"
        ]
    }
]