如何从postman的json响应中提取值,其键为'。在它

时间:2021-08-08 16:05:33

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

从postman的json响应中获取键值是非常直接的:

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

For JSON Response

为JSON响应

{
    "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.

在下面的例子中,我不能提取json值。作为它的一部分。有人能帮我一下吗?

"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);
});

1 个解决方案

#1


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?

如果字符串长度是动态的,还有其他答案吗?

#1


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?

如果字符串长度是动态的,还有其他答案吗?