I need to parse the json reponse payload and replace the values according to pattern search.
example json – {
“a”: {
“first”: {
“f1”: “https://example.com/abc/bcd/ddsdfff/dhssjsksffffdss”,
“f2”: “https://example.com/abc/bcd/wwweejnnsjskndjdk/dhssjsks”,
“f3”: “value3”
},
“t1”: “”,
“f3”: “value3”,
“second”: {
“results”: [
{
“first”: {
“f1”: “https://example.com/abc/bcd/ddsdfff/dhssjsksffffdss”,
“f2”: “https://example.com/abc/bcd/wwweejnnsjskndjdk/dhssjsks”,
“f3”: “value3”
},
“third”: “Data1”,
“twenty”: “data 2”
}
]
}
}
}
Pattern - “/abc/bcd/”
I need to replace the value everywhere in the json payload i.e “https://example.com/abc/bcd/ddsdfff/dhssjsksffffdss” to the value “newvalue”. This value which needs to be replaces can come in any of the property in th json.
Hi @darpan jain
You can try something like this using JavaScript .
// Function call to replace json body
replacejsonresponse();
function replacejsonresponse()
{ // response.content flow variable is holding
// JSon response you want to replace specific regx pattern
var responsecontent = context.getVariable('response.content');
var resultcontent = responsecontent ;
//Regex string for source patternt you want to replace
var sourcepatternstr = "\/abc\/bcd\/";
// new value string which will be replaced
var destvalue = "/newvalue/";
// "g" will do global search and replace
sourcekeyregex = new RegExp(sourcepatternstr,"g");
resultcontent = resultcontent.replace(sourcekeyregex, destvalue);
// Print new Json response body after replacement to check the values
print ("resultcontent " + resultcontent);
context.setVariable('response.content',resultcontent);
}
See if this is what you are after and works for your requirement.
Regards
Jayesh
2 Likes
It is slightly different from what I expected.
The new Json should be :
“a”: { “first”: { “f1”: “newvalue”, “f2”: “newvalue”, “f3”: “value3” }, “t1”: “”, “f3”: “value3”, “second”: { “results”: [ { “first”: { “f1”: “newvalue”, “f2”: “newvalue”, “f3”: “value3” }, “third”: “Data1”, “twenty”: “data 2” } ] } } }
But the new json is something like :
“a”: { “first”: { “f1”: “https://example.com/newvalue/ddsdfff/dhssjsksffffdss”, “f2”: “https://example.com/newvalue/wwweejnnsjskndjdk/dhssjsks”, “f3”: “value3” }, “t1”: “”, “f3”: “value3”, “second”: { “results”: [ { “first”: { “f1”: “https://example.com/newvalue/ddsdfff/dhssjsksffffdss”, “f2”: “https://example.com/newvalue/wwweejnnsjskndjdk/dhssjsks”, “f3”: “value3” }, “third”: “Data1”, “twenty”: “data 2” } ] } } }
Hi @darpan jain
You need to make changes to source regex and destination string to suit your needs. eg for above example try something like this.
replacejsonFinal();
function replacejsonFinal()
{
var responsecontent = context.getVariable('response.content');
var resultcontent = responsecontent ;
var destvalue = "newvalue";
var sourcekeyregex = /[(http(s)?):\/\/(www\.)?a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/ig;
resultcontent = resultcontent.replace(sourcekeyregex, destvalue);
context.setVariable('response.content',resultcontent);
print ("resultcontent " + resultcontent);
}
Hope this makes it bit clear now.
Regards
Jayesh