Announcements
This site is in read only until July 22 as we migrate to a new platform; refer to this community post for more details.
Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Replace URL in Json response

I have a json response as below. I want to replace the URL which exist in JSON response from

https://abc.oktapreview.com/api/

to

https://abc-dallas-ext-non-prod-ingress.ccc.abc.com/v1/conn/api/v1

 

 

 

[
    {
        "id": "00uafl8w1l5E5eeADcx7",
        "status": "ACTIVE",
        "created": "2017-05-10T15:34:18.000Z",
        "activated": "2017-05-10T15:34:18.000Z",
        "statusChanged": "2020-01-08T19:54:55.000Z",
        "lastLogin": "2022-05-17T13:31:39.000Z",
        "lastUpdated": "2022-03-18T12:03:30.000Z",
        "passwordChanged": null,
        "type": {
            "id": "00uafl8w1l5E5eeADcx7"
        },
        "profile": {
            "zzOpCo": "ABC",
            "lastName": "Test",
            "samAccountName": "TEST123",
            "secondEmail": null,
            "zzCountry": "US",
            "login": "test123@mgd.abc.com",
            "zzCity": "HBK",
            "employeeNumber": "1234",            
            "email": "test.a@abc.com"
        },
        "credentials": {
            "provider": {
                "type": "ACTIVE_DIRECTORY",
                "name": "abc.zx.com"
            }
        },
        "_links": {
            "self": {
                "href": "https://abc.oktapreview.com/api/v1/users/00uafl8w1l5E5eeADcx7"
            }
        }
    }
]

 

I think javascript is the way to achieve this. Do you have any sample code which can be used. 

Solved Solved
0 3 2,207
1 ACCEPTED SOLUTION

Will the url appear only in the _links.self.href field? In that case, try this

var c = JSON.parse(context.getVariable('response.content'));
var stringToReplace = 'abc.oktapreview.com/api';
var replacement = 'abc-dallas-whatever.abc.com/v1/whatever';
// assume c is array
c.forEach(function(item) {
  if (item._links.self.href) { 
    item._links.self.href = item._links.self.href.replace(stringToReplace, replacement);
  }
});
context.setVariable('response.content', JSON.stringify(c)); 

If there are other possible items in the links object that might have URLs, then you would need  to generalize the logic in line 6 and 7. 

View solution in original post

3 REPLIES 3