如何在jmeter中删除Json中存在的数组

时间:2019-01-07 07:55:10

标签: jmeter

我有一个json,我必须从中删除整个过滤器块,我该如何删除呢?我在JSR223PostProcessor中尝试了以下操作,但它不起作用

var response = prev.getResponseDataAsString();
var responseWithoutIds = response.replaceAll("\"filter\"[ ]*:[^,}\\]]*[,]?", "");
{
    "templateId": "1e5eef7d-9581-40a1-98ed-774dcf68ce11",
    "ownerId": "241e992f-a1c7-430a-be9f-347337643697",
    "orgId": "4c41a6d7-dfbf-485b-84b6-3a3991546d9c",
    "createdDate": "2018-12-24T10:51:57.336Z",
    "shareWithOrg": false,
    "templateName": "custom template",
    "templateDesc": null,
    "system": false,
    "optionIds": [
        "9c9b93c9-4ada-4489-9129-604cf1fa35be",
        "65285021-9c2c-4787-b810-390c50f48f9f",
        "222c832d-8db6-4ca1-88ef-150b6a388d87",
        "be22ba29-144b-4971-99de-0d4258220558",
        "b7ab883b-fe51-4e02-bdb1-0d7321d30ac3",
        "510660fa-d255-45e2-b6b8-c1c09c9009d6",
        "51f78f08-b655-4a82-b0c5-61365881fb0a",
        "ac1d4b39-f41c-4b69-bdc4-b76fd1d2b9af",
        "2de3591e-c9eb-48bd-a9bf-acfcee695ac8",
        "c9e2a1f1-f786-4e9a-9d11-525a5aa01345",
        "441a0627-9983-4edc-95ce-a5b8e79f7f1d",
        "087a1ac8-e7c1-4cee-ae31-4f0aaccba8c9",
        "dba760fb-9e97-4dcb-b1ab-4a2795e59ee6",
        "53bc5244-b33e-4732-8bf0-cd051e017089",
        "2bf04285-fe19-4534-8467-44c08661da60",
        "020eb946-6ba0-4519-b6f9-23ffafd949d0",
        "ced89c82-5ba1-4fe6-bc83-5bddff820c85",
        "18ea7006-2e29-4a3f-a28f-59a1323a4bd0",
        "c7efa0d1-094c-4626-90aa-80e1343bbdef",
        "6504605f-3a41-4406-9f0e-150365c8ee35",
        "3c28c622-83b1-4b46-ac16-3c5cbff80999",
        "6a227b18-519f-49c4-8c59-7fe0eaf73fea",
        "f9127d4e-c27e-48fa-8286-42a045d8fc40",
        "bb695a06-50fe-4419-924f-7fee96530b63",
        "731393bc-92e0-4ddb-8113-a7b3942527c5",
        "a355ee2d-c654-4406-9027-8e801cb1a4a9"
    ],
    "optionGroupIds": [
        "09f1302b-6ad0-486b-8f8c-e65ae14f5831",
        "2041206d-cc87-4173-912c-fee52cefcf2b",
        "6fc154fb-8313-488c-a2cb-e1a5b88b3028",
        "cc0767f2-3e7f-42f1-b9c7-9f76991a6fcf",
        "4cd75ade-db20-43fe-8b15-7effa961b4b5",
        "6af8ddba-8a7c-4ff7-8043-6358b6e6a31b"
    ],
    "filter": [
        {
            "optionId": "222c832d-8db6-4ca1-88ef-150b6a388d87",
            "selectedValues": [
                "all"
            ],
            "selectedComplexValues": null,
            "contextualFilterId": null,
            "operator": "pastDays",
            "filterType": "date"
        }
    ],
    "sortDirection": "desc",
    "sortedOptionId": "222c832d-8db6-4ca1-88ef-150b6a388d87",
    "templateCategory": "patients"
}

2 个答案:

答案 0 :(得分:0)

使用正则表达式不是最佳的处理方法。还考虑enter image description here语言,它提供了switching to GroovyJsonSlurper类,它们的方式更加可靠。

示例代码:

def response = new groovy.json.JsonSlurper().parse(prev.getResponseData())
response.remove('filter')
def responseWithoutIds = new groovy.json.JsonBuilder(response).toPrettyString()
log.info(responseWithoutIds)   

参考文献:

答案 1 :(得分:0)

在JSR223PostProcessor中使用以下代码:

var body = prev.getResponseDataAsString();
var parsed = JSON.parse(body);
delete parsed.filter[0];
delete parsed.filter;
vars.put('value', JSON.stringify(parsed));