4

It is pretty straight forward to to get a value of a key from json response in postman say :

pm.test("AddCluster", function () {
    var jsonData = pm.response.json();
    pm.globals.set("taskIdCluster", jsonData.value);    
});

For JSON Response

{
    "value": "task-1405"
}

I am not able to extract json value in the below case where key has a '.' as part of its string.Can any one help me with this.

"result": {
        "cluster.moid": "domain-c433242"
    }

I tried the following below code:

pm.test("abc", function () {
    var jsonData = pm.response.json();
    var result = jsonData.result;
    var moid = result.'cluster.moid' ;
    pm.environment.set("clusterMoid", moid);
});
4
  • 1
    have you tried var moid = result.'cluster.moid' ? Commented Oct 16, 2017 at 13:56
  • Thanks Joly it worked! Commented Oct 29, 2017 at 13:44
  • wonderful ! I suggest you edit and upgrade your answer so all the community can benefit from it, thanks ! Commented Oct 30, 2017 at 7:38
  • @A.Joly : :) :) Commented Nov 1, 2017 at 13:10

2 Answers 2

1

Could figure out to extract value for the above case, The below code works

pm.test("StatusForAddClusterApplyCheck", function () {
    var jsonData = pm.response.json();
    var result = jsonData.result;
    var jsonString = JSON.stringify(result).substring(17,31);
    pm.environment.set("clusterMoid", jsonString);
});

But only if the string length are constants.

Any other answer in case string length are dynamic?

Sign up to request clarification or add additional context in comments.

Comments

1

In javascript (And also in postman), object properties can be accessed with '.' operator or with associative array indexing using []. The same goes for JSON objects.

ie. object.key is equivalent to object["key"].

this should do the trick for you:

pm.test("AddCluster", function () {
    var jsonData = pm.response.json();
    pm.globals.set("taskIdCluster", jsonData["cluster.moid"]);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.