Having the following JSON response, how can I get a list of items foo and bar or values of zoneGroup under each element using GPath or RestAssured built-in JsonPath?
{
"priceZones": {
"foo": [
{
"zoneGroup": "foo",
"code": "id1"
}
],
"bar": [
{
"zoneGroup": "bar",
"code": "id2"
}
]
}
}
I tried priceZones.*.[*].zoneGroup JsonPath Evaluator, but get an error that RestAssured doesn't accepts * as a wildcard.
Related
I'm trying to parse a given JSON string and abstract some fields from it.
my Test method looks like this
Steps followed
Making a rest api call and getting response using restassured
response is stored in Response object from restassured
From response ResponseBody is being extracted using getBody() method in ResponseBody object
ResponseBody object is being converted to string using asString()
JsonPath object is being created from string value
Json is parsed by providing Jway Expression
#Test
Response searchResult = this.getEndPoint().SearchRecallAgreement(payload);
ResponseBody b = searchResult.getBody();
String responseBody = b.asString();
JsonPath j = new JsonPath(responseBody);
List<String> ls = j.getList("payload.returnTerms[*].returnTermPolicies[?(#.policyType == 'RC_ITEMS_POLICY')].policies[?(#.policyKey=='/575330172')].policyKey");
System.out.println(ls);
Error in console
Caused by: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 1: Unexpected input: '['; Expecting <EOF> # line 1, column 46.
RootObject.payload.returnTerms[*].return
Json string looks like
{
"status": "OK",
"header": {
"headerAttributes": {}
},
"errors": [],
"payload": {
"totalCount": 6,
"returnTerms": [
{
"returnTermType": "RECALL_AGREEMENT",
"returnTermPolicies": [
{
"policyType": "RC_ITEMS_POLICY",
"keyTemplate": "/itemNo/storeNo",
"policies": [
{
"policyKey": "/583919815",
"attributeList": [
]
},
{
"policyKey": "/575330172",
"attributeList": [
]
},
{
"policyKey": "/583919815/2345"
}
]
}
]
},
{
"returnTermPolicies": [
{
"policyType": "RC_ITEMS_POLICY",
"keyTemplate": "/itemNo/storeNo",
"policies": [
{
"policyKey": "/583919815",
"attributeList": [
]
},
{
"policyKey": "/575330172",
"attributeList": [
]
},
{
"policyKey": "/583919815/2346"
},
{
"policyKey": "/583919815/2345"
}
]
}
]
}
]
}
}
Expected output :
[
"/575330172",
"/575330172"
]
I want to create a report using Jaspersoft Studio with a json file as datasource. I want to select some fields from this json and also a substring of the original json. This should be done by jsonql.
First, an example with the "JSON language" in JasperSoftStudio:
gives the following result:
This is exactly what I want to have, the delivery note number as a field, and the barcodes as an array of objects / json substring.
What I am not able to do is, to achieve this with jsonql. The following query
gives the result
The following json was used for this example
{
"tour": {
"shipments": [
{
"containers": [
{
"boxes": [
{
"customerModule": "DEFG",
"deliveryDateTime": "2022-04-25 11:49:24.834000",
"boxNumber": 2
},
{
"customerModule": "ABCD",
"deliveryDateTime": "2022-04-25 11:50:24.810000",
"boxNumber": 1
}
],
"licensePlate": "123"
}
],
"deliveryNoteNumber": "6785000",
"barcodes": [
{
"content": "barcode_01_04"
},
{
"content": "barcode_03_04"
},
{
"content": "barcode_04_04"
}
]
},
{
"containers": [
{
"boxes": [
{
"customerModule": "ZXYV",
"deliveryDateTime": "2022-04-25 11:51:24.834000",
"boxNumber": 1
},
{
"customerModule": "UHGI",
"deliveryDateTime": "2022-04-25 11:52:24.834000",
"boxNumber": 2
}
],
"licensePlate": "987"
}
],
"deliveryNoteNumber": "6785001",
"barcodes": [
{
"content": "Barcode_01_04"
},
{
"content": "Barcode_02_04"
},
{
"content": "Barcode_04_04"
}
]
}
],
"handlingDateTime": "2022-04-25 11:50:24.883000"
}
I tried to use this documentation, but I could not get it working.
The JSONQL data source doesn't properly convert arrays to Strings for some reason.
What you can do is change the barcodes field from java.lang.String to java.lang.Object. You can do that by clicking Edit in the Fields tab and changing the Class Type for the field.
With java.lang.Object as field type, the field value will be the underlying JSON (Jackson) model array object. You can do $F{barcodes}.toString() if you need to explicitly convert it to a String in an expression.
I am using Gatling to test an API that accepts a json body like below:
{
"data": {
"fields": [
{
"rank": 1
},
{
"name": "Jack"
}
]
}
}
I have created a file feeder.json that contains array of json objects like above.
Below is the feeder.json
[
{
"data": {
"fields": [
{
"rank": 1
},
{
"name": "Jack"
}
]
}
}
]
I have created another file template.txt that contains the template of above json.
Below is the template.txt
{
"data": {
"fields": [
{
"rank": ${data.fields[0].rank} //this is not working
},
{
"name": "Jack"
}
]
}
}
val jsonFeeder = jsonFile("feeder.json").circular
scenario("Test scenario")
.feed(jsonFeeder)
.exec(http("API call test")
.post("/data")
.body(ElFileBody("template.txt"))
.asJson
.check(status is 200))
I am feeding the feeder.json and also sending json body from template.json. The 'rank' property values should get set from feeder into the json body. But I am getting an error 'Map named 'data' does not contain key 'fields[0]'. Stuck with this.
Access by index syntax uses parens, not square braces.
#{data.fields(0).rank}
I'm trying to parse json into kotlin objects but the problem is that its key fields are numbers any idea how can parse them , I've tried serialized name but still facing problem.
The json response looks like this :
{
"Id": [{
"1": {
"name": "name1",
"class": "11a"
}
},
{
"2": {
"name": "name2",
"class": "11b"
}
}
]
}
I'm using gson and the main thing i'm trying to do is to store this number fields as some other string objects.
You can parse them into a list of maps, then "map" those to your data classes instead:
val input = """{
"Id": [{
"1": {
"name": "name1",
"class": "11a"
}
},
{
"2": {
"name": "name2",
"class": "11b"
}
}
]
}"""
val gson = Gson()
val parsed: Map<String, List<Map<String, Any>>> =
gson.fromJson(input, (object : TypeToken<Map<String, List<Map<String, Any>>>>(){}).type)
println(parsed["Id"]?.get(0)?.get("1")) // {name=name1, class=11a}
It will have some nasty generic signature, though.
If you're working with Kotlin, take a look at Klaxon, it will improve your experience.
I'm parsing a JSON document like
{
"status": "ok",
"tick": {
"id": 101006380508,
"data": [
{
"id": "10100638050832281590023",
"amount": 0.2251,
}
]
}
}
I am currently doing:
data = JSON.parse(response.read_body)
data["tick"]["data"]["amount"]
and getting an error
Error: []': no implicit conversion of String into Integer (TypeError)
My objective is the get the amount
It's array that contains one hash, so you can get it as
data["tick"]["data"].first["amount"] # => 0.2251