Search code examples
jsonstringpostmanextract

How to extract string from link in JSON response body; POSTMAN


i want to extract last string from _links.scaStatus.href which is: d186b5de-f05e-43ac-9924-c3f504e81291

{
    "consentStatus": "received",
    "consentId": "12da0088-f48b-4a87-9d9b-29cd66de0825",
    "_links": {
        "self": {
            "href": "/consents/12da0088-f48b-4a87-9d9b-29cd66de0825"
        },
        "status": {
            "href": "/consents/12da0088-f48b-4a87-9d9b-29cd66de0825/status"
        },
        "scaStatus": {
            "href": "/consents/12da0088-f48b-4a87-9d9b-29cd66de0825/authorisations/d186b5de-f05e-43ac-9924-c3f504e81291"
        }
    }
}

Can you please help me?


Solution

  • You can use the Tests tab in your requests and collections to write tests that will execute when Postman receives a response from the API you sent the request to.

    You can write test scripts for your Postman API requests in JavaScript(see this). There are many example in Test script examples.

    For your example use this:

    const responseJson = pm.response.json();
    var test = responseJson._links.scaStatus.href;
    //var test = "/consents/12da0088-f48b-4a87-9d9b-29cd66de0825/authorisations/d186b5de-f05e-43ac-9924-c3f504e81291";
     var arr = test.split("/");
    console.log(arr[4]);