如何从JSON响应中删除空值

时间:2017-08-03 13:51:51

标签: json jmeter performance-testing

我们正在使用Jmeter工具。

我们为请求获取以下JSON响应,我们需要将非null值传递给下一个请求。请建议。

{
    "amount": null,
    "biller": null,
    "billerCategory": null,
    "billerList": null,
    "billerLocation": null,
    "billerLocationList": null,
    "billerNameList": null,
    "blPinValidate": null,
    "category": "Special Tariff Voucher",
    "category_LIST": null,
    "circle": "MaharashtraandGoa",
    "dthNumber": null,
    "isBlLiveBillFetch": false,
    "key_OPERATOR_CIRCLE": null,
    "key_PLAN": [
        "6.00 - Local Mobile calls at 1psec - Talktime :NA - Validity :28",
        "7.00 - Local Mobile calls at 30pmin, First 120 Local seconds of the day at 2psec - Talktime :NA - Validity :28",
        "8.00 - STD Mobile calls at 25pmin, First 120seconds of the day at 2psec - Talktime :NA - Validity :28",
        "16.00 - Local + STD Mobile calls at 1psec - Talktime :NA - Validity :28",
        "17.00 - Local Mobile calls at 30pmin - Talktime :NA - Validity :28",
        "22.00 - Local Vodafone to Vodafone Mobile calls at 10pmin, First 120 Local Vodafone to Vodafone seconds of the day at 2psec - Talktime :NA - Validity :28",
        "23.00 - Unlimited Local + STD Vodafone to Vodafone Calls and 100 MB 3G4G Data (4G Handset) 50 MB for Other Handset for 1 day. - Talktime :NA - Validity :1",
        "24.00 - Local + STD Vodafone calls at 15pmin, first 120 seconds of the day at 2psec + Rs.6 Talktime with 28 days validity - Talktime :6.00 - Validity :28",
        "25.00 - Rs.25 and 2 Local Vodafone Night Minutes - Talktime :25.00 - Validity :3",
        "27.00 - 7500 Seconds to Local Vodafone Mobile - Talktime :NA - Validity :2",
        "34.00 - STD Mobile calls at 25pmin + Rs.5 Talktime - Talktime :5.00 - Validity :28",
        "35.00 - Local + STD Mobile calls at 30pmin - Talktime :NA - Validity :28",
        "38.00 - Local Mobile calls at 30pmin - Talktime :NA - Validity :56",
        "41.00 - Local Vodafone to Vodafone Mobile calls at 10pmin - Talktime :NA - Validity :28",
        "42.00 - STD Mobile Calls at 25pmin - Talktime :NA - Validity :28",
        "45.00 - Unlimited Local + STD Vodafone to Vodafone Calls and 250 MB 3G4G Data (4G Handset) 50 MB for Other Handset for 7 days. - Talktime :NA - Validity :7",
        "54.00 - Talktime of Rs.43.96 and 1 Local Vodafone Night Mins - Talktime :43.96 - Validity :NA",
        "57.00 - Local + STD Mobile calls at 30pmin + 5 Local Night Vodafone to Vodafone Minutes - Talktime :NA - Validity :28",
        "60.00 - Get Talktime between Rs.51 and Rs.100 - Talktime :51.00 - Validity :NA",
        "65.00 - Rs.65 and 2 Local Vodafone Night Minutes - Talktime :65.00 - Validity :6",
        "69.00 - 15000 Local Vodafone to Vodafone Mobile seconds - Talktime :NA - Validity :7",
        "89.00 - Unlimited Local + STD Vodafone to Vodafone + 100min Vodafone to Other Local + STD Calls and 250 MB 3G4G Data (4G Handset) 50 MB for Other Handset for 7 days. - Talktime :NA - Validity :7",
        "91.00 - 18000 Local Vodafone to Vodafone Mobile seconds - Talktime :NA - Validity :10",
        "98.00 - 12500 Local + STD Mobile Seconds. - Talktime :NA - Validity :28",
        "103.00 - Local + STD Vodafone to Vodafone Mobile calls at 15pmin - Talktime :NA - Validity :28",
        "106.00 - 250 MB Data + Talktime Rs.25. Special features included call 1 Local Vodafone buddy at 10pmin, option of 60 MB Data Loan, Free subscription to Saavn Pro. - Talktime :25.00 - Validity :28",
        "118.00 - Get 325 FLEX at Rs.118, Use Calls, Internet, SMS and Roaming with one recharge. How to use FLEX 1 FLEX 1 MB Internet1 SMS1min Incoming Roaming, 2 FLEX 1min LocalSTDOutgoing Roaming. - Talktime :NA - Validity :28",
        "148.00 - Unlimited Local + STD Vodafone to Vodafone Mobile Calls, Other Calls at 1psec + 300 MB Data for 4G Handsets50 MB Data for non-4G handsets, SMS at 25p FUP for Vodafone to Vodafone Calls-300 minday, 1200 minweek, Calls to 300 unique numbersweek, charge of 10pm beyond this. - Talktime :NA - Validity :28",
        "178.00 - 500 MB Data + Talktime Rs.30. Special features included call 1 Local Vodafone buddy at 10pmin, option of 60 MB Data Loan, Free subscription to Saavn Pro. - Talktime :30.00 - Validity :28",
        "194.00 - Get 700 FLEX at Rs.194, Use Calls, Internet, SMS and Roaming with one recharge. How to use FLEX 1 FLEX 1 MB Internet1 SMS1min Incoming Roaming, 2 FLEX 1min LocalSTDOutgoing Roaming. - Talktime :NA - Validity :28",
        "203.00 - 27000 Local + STD Mobile seconds - Talktime :NA - Validity :28",
        "225.00 - 22000 Local + STD Mobile seconds - Talktime :NA - Validity :28",
        "296.00 - Get 1200 FLEX at Rs.296, Use Calls, Internet, SMS and Roaming with one recharge. How to use FLEX 1 FLEX 1 MB Internet1 SMS1min Incoming Roaming, 2 FLEX 1min LocalSTDOutgoing Roaming. - Talktime :NA - Validity :28",
        "298.00 - 1536 MB Data + Talktime Rs.50. Special features included call 1 Local Vodafone buddy at 10pmin, option of 60 MB Data Loan, Free subscription to Saavn Pro. - Talktime :50.00 - Validity :28",
        "346.00 - Unlimited Local + STD Calling for 28 days with Daily capping of 300 Minutes per day. Additional 50 MB Data for 28 days. - Talktime :NA - Validity :28",
        "348.00 - Unlimited Local + STD Calling for 28 days with Daily capping of 300minutes per day. Additional 50 MB Data for 28 days. - Talktime :NA - Validity :28",
        "349.00 - All calls Unlimited including Landline + 1 GB 3G Data for 4G Handsets50 MB data for non-4G handsets, FUP - 300minday, 1200minweek, calls to 300 unique numbersweek, charge of 30pmin beyond this. - Talktime :NA - Validity :28",
        "398.00 - Get 1750 FLEX at Rs.398, Use Calls, Internet, SMS and Roaming with one recharge. How to use FLEX 1 FLEX 1 MB Internet1 SMS1min Incoming Roaming, 2 FLEX 1min LocalSTDOutgoing Roaming. - Talktime :NA - Validity :28",
        "499.00 - 700 Local and STD min + 2 GB 3G Data - Talktime :NA - Validity :28",
        "501.00 - Full Talktime 0 validity + 10 Vodafone to Vodafone Night mins - Talktime :501.00 - Validity :NA",
        "699.00 - 1000 Local and STD min + 4 GB 3G Data - Talktime :NA - Validity :28",
        "715.00 - Talktime of Rs.618.74 + 2 Night Vodafone to Vodafone Minute for 1 day - Talktime :618.74 - Validity :28",
        "998.00 - 2000 Local and STD min + 6 GB 3G Data - Talktime :NA - Validity :28",
        "1001.00 - Full Talktime, 0 validity + 10 Vodafone Night mins - Talktime :1001.00 - Validity :NA",
        "1098.00 - 3 GB 3G + Unlimited Local Vodafone to Vodafone Calls - Talktime :NA - Validity :28",
        "1299.00 - 3000 Local and STD min + 8 GB 3G Data - Talktime :NA - Validity :28",
        "1749.00 - 5 GB 3G + Unlimited Local Calls - Talktime :NA - Validity :28",
        "2249.00 - 7 GB 3G + Unlimited Local and STD Calls - Talktime :NA - Validity :28",
        "3249.00 - 15 GB 3G + Unlimited Local and STD Calls + Free Incoming on Roaming at Rs.3249 - Talktime :NA - Validity :28"
    ],
    "location": null,
    "mobileNo": "9778697852",
    "objBusBillPaymentBean": null,
    "objBusTransferBean": null,
    "operator": "VODAFONE PREPAID",
    "plan": null,
    "rechargeMobileNo": "9637115216",
    "rechargeType": "MOBILE_RECHARGE",
    "ref1": null,
    "ref2": null,
    "ref3": null,
    "ref4": null,
    "ref5": null,
    "responseMessage": null,
    "stCurBal": null,
    "stRandNum": null,
    "stUserType": null,
    "stValue1": null,
    "stValue2": null,
    "stValue3": null,
    "stValue4": null,
    "stValue5": null,
    "wpin": null
}

1 个答案:

答案 0 :(得分:0)

  1. 添加JSR223 PostProcessor作为返回此JSON响应的请求的子项
  2. 将以下代码放入“脚本”区域:

    def json = new groovy.json.JsonSlurper().parse(prev.getResponseData())
    
    for (Iterator<Map.Entry> it = json.entrySet().iterator(); it.hasNext();) {
        Map.Entry entry = it.next();
        if (entry.getValue() == null) {
            it.remove()
        }
    }
    
    vars.put("data", new groovy.json.JsonBuilder(json).toPrettyString())
    
  3. 没有“null”元素的JSON数据将作为${data} JMeter变量提供。

  4. 参考文献: