英文:
Get the array object based on matching key in groovy
问题
Response:
{
"Success": true,
"RoomRecordsCount": 0,
"RoomBookings": [],
"DeskRecordsCount": 1,
"DeskBookings": [
{
"BookingID": 569114,
"BookingType": 2,
"BookingStart": "11/07/2023 12:00 PM",
"BookingEnd": "11/07/2023 23:59 PM",
"TimeZoneOffSetInMins": 60,
"TimeZone": "GMT Standard Time",
"Status": 3,
"AdditionalInformation": null,
"FloorID": 0,
"FloorName": "0",
"CountryID": 9,
"LocationID": 93,
"LocationName": "AJ-LOC",
"GroupID": 259,
"GroupName": "AJ-GROUP",
"DeskID": 29264,
"DeskName": "Desk 2",
"CanBeBooked": false,
"DeskAttributes": null,
"WSTypeId": 2,
"WsTypeName": "MS Desk",
"QRCodeEnabled": false,
"bookMultipleDesk": true
}
],
"EditMiniSyncBooking": false,
"Error": {
"ErrorCode": 0,
"ErrorDescription": ""
}
}
Error:
2023-07-11 00:07:57,966 ERROR o.a.j.e.JSR223PostProcessor: Problem in JSR223 script, JSR223 PostProcessor
javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: DeskBookings for class: java.util.LinkedHashMap$Entry
Groovy code:
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
def jsonSlurper = new JsonSlurper()
def response = jsonSlurper.parseText(prev.getResponseDataAsString())
def deskBookings = response.DeskBookings
def filteredDeskBooking = deskBookings.find { it.DeskID == '29264' }
def json = JsonOutput.toJson(filteredDeskBooking)
vars.put("bookingId", json.BookingID)
英文:
I have an HTTP request in the Jmeter, for which I am getting a JSON response consisting of an array.
Response:
{
"Success": true,
"RoomRecordsCount": 0,
"RoomBookings": [],
"DeskRecordsCount": 1,
"DeskBookings": [
{
"BookingID": 569114,
"BookingType": 2,
"BookingStart": "11/07/2023 12:00 PM",
"BookingEnd": "11/07/2023 23:59 PM",
"TimeZoneOffSetInMins": 60,
"TimeZone": "GMT Standard Time",
"Status": 3,
"AdditionalInformation": null,
"FloorID": 0,
"FloorName": "0",
"CountryID": 9,
"LocationID": 93,
"LocationName": "AJ-LOC",
"GroupID": 259,
"GroupName": "AJ-GROUP",
"DeskID": 29264,
"DeskName": "Desk 2",
"CanBeBooked": false,
"DeskAttributes": null,
"WSTypeId": 2,
"WsTypeName": "MS Desk",
"QRCodeEnabled": false,
"bookMultipleDesk": true
}
],
"EditMiniSyncBooking": false,
"Error": {
"ErrorCode": 0,
"ErrorDescription": ""
}
}
This 'DeskBookings' can have multiple objects in its array. So I want only that object which matches with specific 'DeskID'
I wrote the groovy code in 'JSR223 PostProcessor'
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
def jsonSlurper = new JsonSlurper();
def response = jsonSlurper.parseText(prev.getResponseDataAsString());
def json = JsonOutput.toJson(response.findAll{ it.DeskBookings.DeskID == '29264' });
vars.put("bookingId", json.BookingID);
Error:
2023-07-11 00:07:57,966 ERROR o.a.j.e.JSR223PostProcessor: Problem in JSR223 script, JSR223 PostProcessor
javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: DeskBookings for class: java.util.LinkedHashMap$Entry
答案1
得分: 1
有几个问题:
- 你应该使用DeskBookings中的.find方法
- DeskID是一个数字
这是一个小脚本,用于说明这些更改。
import groovy.json.*
String json = '''
{
"Success": true,
"RoomRecordsCount": 0,
"RoomBookings": [],
"DeskRecordsCount": 1,
"DeskBookings": [
{
"BookingID": 569114,
"BookingType": 2,
"BookingStart": "11/07/2023 12:00 PM",
"BookingEnd": "11/07/2023 23:59 PM",
"TimeZoneOffSetInMins": 60,
"TimeZone": "GMT Standard Time",
"Status": 3,
"AdditionalInformation": null,
"FloorID": 0,
"FloorName": "0",
"CountryID": 9,
"LocationID": 93,
"LocationName": "AJ-LOC",
"GroupID": 259,
"GroupName": "AJ-GROUP",
"DeskID": 29264,
"DeskName": "Desk 2",
"CanBeBooked": false,
"DeskAttributes": null,
"WSTypeId": 2,
"WsTypeName": "MS Desk",
"QRCodeEnabled": false,
"bookMultipleDesk": true
}
],
"EditMiniSyncBooking": false,
"Error": {
"ErrorCode": 0,
"ErrorDescription": ""
}
}
'''
def jsonSlurper = new JsonSlurper()
def response = jsonSlurper.parseText(json);
def desk = JsonOutput.toJson(response.DeskBookings.find{ it.DeskID == 29264 });
println(desk)
英文:
There were a couple of problems:
- You should use .find from DeskBookings
- The DeskID is a number
Here is a little script to illustrate the changes.
import groovy.json.*
String json = '''
{
"Success": true,
"RoomRecordsCount": 0,
"RoomBookings": [],
"DeskRecordsCount": 1,
"DeskBookings": [
{
"BookingID": 569114,
"BookingType": 2,
"BookingStart": "11/07/2023 12:00 PM",
"BookingEnd": "11/07/2023 23:59 PM",
"TimeZoneOffSetInMins": 60,
"TimeZone": "GMT Standard Time",
"Status": 3,
"AdditionalInformation": null,
"FloorID": 0,
"FloorName": "0",
"CountryID": 9,
"LocationID": 93,
"LocationName": "AJ-LOC",
"GroupID": 259,
"GroupName": "AJ-GROUP",
"DeskID": 29264,
"DeskName": "Desk 2",
"CanBeBooked": false,
"DeskAttributes": null,
"WSTypeId": 2,
"WsTypeName": "MS Desk",
"QRCodeEnabled": false,
"bookMultipleDesk": true
}
],
"EditMiniSyncBooking": false,
"Error": {
"ErrorCode": 0,
"ErrorDescription": ""
}
}
'''
def jsonSlurper = new JsonSlurper()
def response = jsonSlurper.parseText(json);
def desk = JsonOutput.toJson(response.DeskBookings.find{ it.DeskID == 29264 });
println(desk)
答案2
得分: 1
首先,您无需编写任何脚本,可以使用 JSON 提取器 获取BookingID
。
如果您仍然希望继续使用Groovy,请考虑以下几点:
- 您的
DeskID
是一个整数,需要删除引号。 - 无需使用JsonOutput。
- vars.put() 函数需要两个字符串,而您试图放入一个整数。
您需要按照以下方式修改您的代码:
def jsonSlurper = new JsonSlurper();
def response = jsonSlurper.parse(prev.getResponseData());
def deskBooking = response.DeskBookings.find {it.DeskID == 29264}
vars.put("bookingId", deskBooking.BookingID as String);
更多信息:
英文:
First of all you don't need to do any scripting, you can get the BookingID
using JSON Extractor
If you still want to continue using Groovy consider following points:
- Your
DeskID
is an integer, you need to remove quotation marks from there - There is no need to use JsonOutput
- vars.put() function expects 2 Strings and you're trying to put an integer there
You need to amend your code as follows:
def jsonSlurper = new JsonSlurper();
def response = jsonSlurper.parse(prev.getResponseData());
def deskBooking = response.DeskBookings.find {it.DeskID == 29264}
vars.put("bookingId", deskBooking.BookingID as String);
More information:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论