Replace string from response in test

Hi,

I want to replace some string from a repsonse and use it as request body in another request by setting it as pm variable as below: -

I want to replace string
{“contextKey”:“xxxxyyyzzz”,“result”: {

with string
{“basketAction”: “AddAfterConfig”, “productConfig”: {

in the response of a request, here contextKey value “xxxxyyyzzz” is dynamic and changes in each response.

I tried using method substringAfter but it didn’t work…

Can you share the full response from the first request, and a full example of the second request.

You don’t appear to be posting strings, but objects that contain key\value pairs.

“contextKey”:“xxxxyyyzzz”,“result”: {

contextKey is is the key, and “xxxxyyyzzz” is the value.
results is also a key, which appears to be pointing to another object as the value.

Be specific if its just a single value you want to change, the key name, or a combination of the two.

Changing values is easy. Changing key names is a bit harder, as there is no rename function, so you have to copy and then delete the original.

If its just the value for contextKey, this should be straight forward, but please include your examples so we can confirm.

Please use the preformatted text option in the editor when pasting code, so it doesn’t all align to the left.

Hi mdjones,

PFB the response of 1st request and the request body which i need for next request.

Response body from 1st request
{
“contextKey”: “fcc436df1f99f1b580df0d440e72c28c”,
“result”: {
“offerDetails”: {
“offer”: {
“addtocart”: {
“rest”: {
“params”: {
“context”: “{"CATALOG_ID":"Affiliate"}”,
“basketAction”: “AddWithNoConfig”,
“offer”: “ABC00011”
},
“link”: “v3/catalogs/Deals/basket”,
“method”: “POST”
}
}
}
}
}
}

Request body required for next request

{
“basketAction”: “AddAfterConfig”,
“productConfig”: {
“offerDetails”: {
“offer”: {
“addtocart”: {
“rest”: {
“params”: {
“context”: “{"CATALOG_ID":"Affiliate"}”,
“basketAction”: “AddWithNoConfig”,
“offer”: “ABC00011”
},
“link”: “v3/catalogs/Deals/basket”,
“method”: “POST”
}
}
}
}
}
}

First of all, you haven’t use the preformatted text option so everything is aligned to the left which is hard to read.

Please re-post using the preformatted text, so its easier to see objects within object, etc. It helps a lot when targeting elements. It also helps if I’m going to cut and paste it into Postman. I’m not going to copy\paste, and then spend another five minutes re-formatting the code so its readable. If you don’t use the preformatted text option, then all of the quotes gets transposed and have to changed back.

I am assuming here that you want to take an element from the 1st response to use in the 2nd request, but re-reading your original question I’m actually unsure if this is what you want.

So can I please ask again for you to be as specific as possible.

What key specifically in the request body do you want to update?

And which value from the response do you want to use to update the key?

Hi Mike

Thanks for your reply, I am trying to use the response from one request as body of another request in a collection but before using the the response, I want to edit it as below in Pre-request Script of 2nd request: -

Response of 1st request: -

{
  "contextKey": "344aa688e43ef2b49538de8f01bb0330",
  "result": {
    "offerDetails": {
      "offer": {
        "addtocart": {
          "rest": {
            "params": {
              "context": "{\"AFFILIATES_ID\":\"Affiliate\"}",
              "basketAction": "AddWithNoConfig",
              "offer": "AB000011"
            },
            "link": "v3/catalogs/BroadbandDeals/basket",
            "method": "POST"
          }
        }
      }
    }
  }
}

Request body needed for 2nd request

{
  "basketAction": "AddAfterConfig",
  "productConfig": {
    "offerDetails": {
      "offer": {
        "addtocart": {
          "rest": {
            "params": {
              "context": "{\"AFFILIATES_ID\":\"Affiliate\"}",
              "basketAction": "AddWithNoConfig",
              "offer": "AB000011"
            },
            "link": "v3/catalogs/BroadbandDeals/basket",
            "method": "POST"
          }
        }
      }
    }
  }
}

which is basically replacing

{
  "contextKey": "344aa688e43ef2b49538de8f01bb0330",
  "result": {

with

{
  "basketAction": "AddAfterConfig",
  "productConfig": {

Hope I am able to explain my requirement well.

Your initial response only has two top level keys.

JSON objects are key value pairs, but the values can be strings or another object.

Your response is an object with two keys in it.

contextKey is a string.
results is an object.

If you replace the values for contextKey and results, then you are replacing the entire object.

I’m guessing what you want to do is retrieve the offerDetails element from the response and use this in the body of the 2nd request.

In the first request, you would save this to a collection or environment variable.
Please remember to Stringify when saving JSON to a variable.

In the body of the second request, you can just point to the variable using curly braces.

Request 1 - Test Tab

const response = pm.response.json()

pm.collectionVariables.set('offerDetails', JSON.stringify(response.result.offerDetails));

console.log(pm.collectionVariables.get('offerDetails'));

Request 2 - Body

{
  "basketAction": "AddAfterConfig",
  "productConfig": {
    "offerDetails": {{offerDetails}}
  }
}

Thanks Mike,
It worked… :slight_smile:

Am also having the same doubt and also I want to update the field “Affiliats_id” with “hello” , can you help me on this. .

{
“basketAction”: “AddAfterConfig”,
“productConfig”: {
“offerDetails”: {
“offer”: {
“addtocart”: {
“rest”: {
“params”: {
“context”: “{"AFFILIATES_ID":"Affiliate"}”,

@supply-architect-183

Your problem is that context is an object within a string, instead of a straight up object.

const response = {
    "basketAction": "AddAfterConfig",
    "productConfig": {
        "offerDetails": {
            "offer": {
                "addtocart": {
                    "rest": {
                        "params": {
                            "context": '{"AFFILIATES_ID":"Affiliate"}'
                        }
                    }
                }
            }
        }
    }
}

// console log initial response
console.log(response);
// context is an object in a string
// so we will parse it to an object.
let context = JSON.parse(response.productConfig.offerDetails.offer.addtocart.rest.params.context);
// and console log it.
console.log(context);
// we can now update the elements directly.
context.AFFILIATES_ID = "Hello";
console.log(context);
// before updating the context in the intial response - remembering to stringify it.
response.productConfig.offerDetails.offer.addtocart.rest.params.context = JSON.stringify(context);
console.log(response);

and the various console logs.

image