How to traverse thru a response when it is as below - karate

{
"createDate": "2019-05-15 10:07:44",
"mak": "pokijoklm",
"optStatus": "yujuim",
"partnerAccount": {
"operatorName": "frftcrtii",
"partnerCustomerId": "XXXXX",
"type": "partnerAccount"
},
"transactionId": "hjknhijn1",
"type": "nihnui",
"updateDate": "2019-05-15 11:20:59"
}
For an array we can try as response.partneraccount[*]. but my response is { }

You use [] only when there are arrays. Just observe your JSON structure carefully and you will get it. Try pasting the below into a fresh Scenario and see it work:
* def response =
"""
{
"createDate": "2019-05-15 10:07:44",
"mak": "pokijoklm",
"optStatus": "yujuim",
"partnerAccount": {
"operatorName": "frftcrtii",
"partnerCustomerId": "XXXXX",
"type": "partnerAccount"
},
"transactionId": "hjknhijn1",
"type": "nihnui",
"updateDate": "2019-05-15 11:20:59"
}
"""
* def partnerAccount = response.partnerAccount
* match partnerAccount == { operatorName: 'frftcrtii', partnerCustomerId: 'XXXXX', type: 'partnerAccount' }
* def custId = response.partnerAccount.partnerCustomerId
* match custId == 'XXXXX'

Related

How to get object from array when you don't know the index number

so here is my Actual response of get call
{"message":
{ "Name": "Rodger",
"Salary": "5000"
"id": 54g545f5dfd5e5d
},
{ "Name": "Novak",
"Salary": "4000"
"id": 62g345f7dfd3e5g
}
}
later using post call I added another person who has same name and salary. and its get call response look like
{"message":
{ "Name": "Rodger",
"Salary": "5000"
"id": 54g545f5dfd5e5d
},
{ "Name": "Rodger",
"Salary": "5000"
"id": 27g145f5dfd4e5k
},
{ "Name": "Novak",
"Salary": "4000"
"id": 62g345f7dfd3e5g
}
}
So now I want to get that object who's id is 27g145f5dfd4e5k.
Note: I don't know the index so using id can I get that person.
Thank you in advance!
I recommend you spend some time understanding JSON and how to use JS in Karate to access data.
* def response = { message: [ { name: 'foo1', id: 'bar1' }, { name: 'foo2', id: 'bar2' } ] }
* def id1 = response.message[0].id
* match id1 == 'bar1'
* def id2 = response.message[1].id
* match id2 == 'bar2'

Karate - How change key name in JSON

I have the following JSON. I want to change the keyName 'freeDelivery' to 'isFreeDelivery' but I can't figure out how to do it.
{
"result": [
{
"deliverySlots": [
{
"id": "2DNN",
"date": "2022-04-05",
"freeDelivery": false,
"label": "All day delivery 08:30am to 5pm",
"price": "£5.00",
"fullSlotId": "2DNN"
},
{
"id": "2DPM",
"date": "2022-04-05",
"freeDelivery": false,
"label": "Afternoon 12pm to 5pm",
"price": "£10.00",
"fullSlotId": "2DPM"
}
]
},
{
"deliverySlots": [
{
"id": "2DNN",
"date": "2022-04-06",
"freeDelivery": false,
"label": "All day delivery 08:30am to 5pm",
"price": "£5.00",
"fullSlotId": "2DNN"
},
{
"id": "2DPM",
"date": "2022-04-06",
"freeDelivery": false,
"label": "Afternoon 12pm to 5pm",
"price": "£10.00",
"fullSlotId": "2DPM"
}
]
}
]
}
I've looked at the following pages but still can't figure out how to do it. Do I have to do a transorm or is there an easier way?
https://github.com/karatelabs/karate/blob/master/karate-junit4/src/test/java/com/intuit/karate/junit4/demos/js-arrays.feature
https://github.com/karatelabs/karate#json-transforms
Here you go:
* def payload = { before: 'foo' }
* remove payload.before
* payload.after = 'bar'
* match payload == { after: 'bar' }
Instead of remove this will also work (using pure JS):
* eval delete payload.before
EDIT: after seeing the comments, I would treat this as a JSON transform.
* def payload = { before: 'foo' }
* def fun = function(x){ var res = {}; res.after = x.before; return res }
* def result = fun(payload)
* match result == { after: 'foo' }
I'm sure now you'll want to "retain" all the existing data. Fine, here you go:
* def payload = { before: 'foo' }
* def fun = function(x){ var res = x; res.after = x.before; delete res.before; return res }
* def result = fun(payload)
* match result == { after: 'foo' }
And you already know that you can run a transform on all array elements like this:
* def result = karate.map(someArray, fun)
Please note that you can create 2 or 3 transforms - and "nest" them.

Karate - filter a specific json key from response based on another static array

I have the following JSON response (reference name: "list") and
[
{
"key": "101",
"val": {
"portCall": {
"id": 12664978
},
"status": "in-port"
}
},
{
"key": "102",
"val": {
"portCall": {
"id": 12415798
},
"status": "in-port"
}
},
{
"key": "103",
"val": {
"status": "on-voyage",
"voyage": {
"id": "7kynv-7lq85"
}
}
},
{
"key": "104",
"val": {
"status": "on-voyage",
"voyage": {
"id": "7kynv-2385"
}
}
}
]
also, I have an array list of few key values, evImos = [101,102,104]
In that, I have to identify the first key in the "list" response that has status as "on-voyage". So, the result should be "104".
I have tried the following and I need some help to make it work. Any help would be appreciated.
* def function getFirst = function(evImos) { for (let num of evImos) { let results = list.filter(d => d["key"] === num && d["val"]["status"] === "on-voyage"); if(results.length === 1) { karate.log(num); return num; } } }
* list.forEach(getFirst(evImos))
I'll just give you one hint. This one line will convert the whole thing in to a form that is much easier for you to validate:
* def temp = {}
* list.forEach(x => temp[x.key] = x.val.status)
Which gives you:
{
"101": "in-port",
"102": "in-port",
"103": "on-voyage",
"104": "on-voyage"
}
Now you can do:
* def isOnVoyage = function(key){ return temp[key] == 'on-voyage' }
Also read this: https://stackoverflow.com/a/59162760/143475
Thanks, to #Peter.
Based on his hint, I just tweaked it a little bit to match my requirement and it worked for me.
Here is the working copy for anyone to refer in the future.
* def temp = {}
* list.forEach(x => temp[x.key] = x.val.status)
* def isOnVoyage = function(keys){ for (let key of keys) { if(temp[key] == 'on-voyage'){ karate.log(key); karate.set('num', key); break; }}}
* isOnVoyage(evImos)

Extract data from json array in Karate

In the below JSON response, I need to extract the 'cid' for the record that has the 'nationalityDecription' as 'USA'. By using this query as a reference, I used the below loc in the karate feature file, but 1st line itself fails with syntax error(tried different combinations). For now, I'm using the custom javascript as a workaround which is working fine. I need help to check if i'm missing anything in syntax. Thanks
Response:
{
"header": {
"Id": "12345678",
"timeStamp": "2018-09-17T10:09:812.000"
},
"dataRecords": [
{
"cid": "31H678",
"cidMeta": "00",
"nationalityDecription": "CHINA"
},
{
"cid": "31S421",
"cidMeta": "01",
"nationalityDecription": "USA"
}
]
}
Feature file:
* def record= $response.dataRecords[?(#.nationalityDecription=='USA')]
* def cid = record.cid
* def response = { "header": { "Id": "12345678", "timeStamp": "2018-09-17T10:09:812.000" }, "dataRecords": [ { "cid": "31H678", "cidMeta": "00", "nationalityDecription": "CHINA" }, { "cid": "31S421", "cidMeta": "01", "nationalityDecription": "USA" } ] }
* def cid = get[0] response.dataRecords[?(#.nationalityDecription=='USA')].cid
* match cid == '31S421'

Karate - Validating schema for the entire json response

I am working on validating the entire json response for a GET request using Karate.
Here is the sample json response from the request (I have shown only two elements for items )
[
{
"items": [
{
"createdById": "ADMIN",
"createdByName": "ADMIN",
"changedByName": "ADMIN",
"oid": "121212fgfg2123",
"links": {
"self": {
"href": "/internal/organiz/12345"
},
"subcom": []
},
"name": "NewETLTesting"
},
{
"createdById": "ADMIN",
"createdByName": "ADMIN",
"changedByName": "ADMIN",
"oid": "1212dfffg45",
"links": {
"self": {
"href": "/internal/organiz/5a6e0"
},
"subcom": []
},
"name": "FromKarateModified"
}
]
}
]
Here is how I am trying to validate:
And match response.*.* ==
"""
{
"createdById" : '#string',
"createdByName" : '#string',
"changedByName" : '#string',
"oid" : '#string',
"links" : '#object',
"name" : '#string'
}
"""
However, I am getting an assertion error:reason: actual value is not map-like. If I try putting square brackets around the curly braces, I get reason: actual and expected arrays are not the same size. Also I tried to play around with response like $.[*]. etc but couldn't get it working.
Thanks in advance!!
You need to pay attention to your JSON structure and also understand JsonPath better. You can cut and paste the below and see it working:
* def response =
"""
[
{
"items": [
{
"createdById": "ADMIN",
"createdByName": "ADMIN",
"changedByName": "ADMIN",
"oid": "121212fgfg2123",
"links": {
"self": {
"href": "/internal/organiz/12345"
},
"subcom": []
},
"name": "NewETLTesting"
},
{
"createdById": "ADMIN",
"createdByName": "ADMIN",
"changedByName": "ADMIN",
"oid": "1212dfffg45",
"links": {
"self": {
"href": "/internal/organiz/5a6e0"
},
"subcom": []
},
"name": "FromKarateModified"
}
]
}
]
"""
And match each response[0].items ==
"""
{
"createdById" : '#string',
"createdByName" : '#string',
"changedByName" : '#string',
"oid" : '#string',
"links" : '#object',
"name" : '#string'
}
"""
The below Schema will validate the entire reponse properly
* def refSubcom = {<object schema>}
* def refself = {href : '#string'}
* def refLinks = {self : '#object refself', subcom:'##[] refSubcom'}
* def optionalItemArr =
"""
{
createdById:'#string',
createdByName:'#string',
changedByName:'#string',
oid: '#string',
links: '#object refLinks',
name:'#string'
}
###
* def itemData =
"""
{
item : '##[] optionalItemArr'
}
"""
* def reponseSchema = '##object itemData'
* def SuccessSchema = '##[] reponseSchema'
you can refer the below link : karate : Complex JSON Schema matching