I would like to print the index value for matching JSON data using Karate.
For below code the expected answer should be 2, but I got -1, not sure what I am missing
[
{"aaa": 101},
{"bbb": 102},
{"ccc": 103}
]
Feature: Rough
Scenario: Rough
* def myData = read('roughTestData.json')
* print "Index ->>", myData.indexOf('ccc')
You have a JSON object within an array (not a plain string), and the complication here is you are interested in keys not values, so you need to do this instead:
* def response = [{"aaa":101},{"bbb":102},{"ccc":103}]
* def keys = response.map(x => Object.keys(x)[0])
* def index = keys.indexOf('ccc')
* match index == 2
We are using JS array methods such as map(), Object.keys() and indexOf() above
That said, I personally think you are over-complicating things because Karate allows you to match without caring about the order. For example:
* def response = [{"aaa":101},{"bbb":102},{"ccc":103}]
* def expected = [{ccc:'#number'},{bbb:'#number'},{aaa:'#number'}]
* match response contains only expected
So read the docs, and be creative: https://github.com/karatelabs/karate#json-arrays
I have a tricky authentication issue which requires multiple calls to ultimately obtain an access-token.
I am currently stumbling on the conversion of a returned correlation-id to a correct format.
The correlationID from a post response is returned as: Id-c5ea93607b0682a76040b5db 0; Id-c5ea93607b0682a76040b5db 2
I need to convert to c5ea93607b0682a76040b5db
I have tried with * def serviceId1 = correlationID.substring(correlationID.lastIndexOf('-')+ 1).trim()
which is resulting in c5ea93607b0682a76040b5db 2
I need to strip the " 2" value from the backend of the id to make it
from: 'c5ea93607b0682a76040b5db 2'
to: 'c5ea93607b0682a76040b5db'
I have tried many ways but cant refine to get working - ANY HELP IS MUCH APPRECIATED
The code below:
Scenario: Obtaining access_token
* def correlationID = 'Id-c5ea93607b0682a76040b5db 0; Id-c5ea93607b0682a76040b5db 2'
* def serviceId1 = correlationID.substring(correlationID.lastIndexOf('-')+ 1).trim()
* print 'correlationID ' + correlationID
* print 'serviceID ' + serviceId1
is resulting in c5ea93607b0682a76040b5db 2 ( I want to strip the trailing " 2" - how can I do this?
Here you go:
* def temp = 'c5ea93607b0682a76040b5db 2'
* def pos = temp.lastIndexOf(' ')
* def fixed = temp.substring(0, pos)
* match fixed == 'c5ea93607b0682a76040b5db'
I am getting a JSON response with JSONArray having leading zeros. Example, registrationnumber = ["0000012345", "0000001234", "000123456"]
I want to remove these zeros and compare with another json response where I get the registrationNumber =["12345", "1234", "123456"]
I am trying to do this with my automation in karate-dsl.
Can someone help?
Thanks.
To convert to a number just multiply by 1. You should read the docs: https://github.com/intuit/karate#type-conversion
* def data = ["0000012345", "0000001234", "000123456"]
* def nums = karate.map(data, function(x){ return parseInt(x) })
* match nums == [12345, 1234, 123456]
This question already has an answer here:
Is there numeric value comparison available in Karate testing framework?
(1 answer)
Closed 2 years ago.
How to convert this "2203" to 2203
Tried with some given examples, no Luck
Jobid="2203"
* def Jobid = response # Jobid="2203"
* def intJobid = function(x){ x.Jobid = ~~x.Jobid; return x }
* def results = karate.map(intJobid, Jobid)
* match results == #number
Use parseInt() to convert string to number. An alternative is to just multiply the string with 1. e.g.
* def foo = '10'
* string json = { bar: '#(1 * foo)' }
* match json == '{"bar":10.0}'
* string json = { bar: '#(parseInt(foo))' }
* match json == '{"bar":10.0}'
Source: https://intuit.github.io/karate/#floats-and-integers
i have a json as follows:
* def first = [{"country":"X","code":"XY","cityName":"XYZN","city":"XYZ","timezone":"XYZT","latitude":"57.0928","name":"XYZN","longitude":"9.8492"}, {"country":"A","code":"AB","cityName":"ABCN","city":"ABC","timezone":"ABCT","latitude":"1.234","name":"ABCN","longitude":"29.8482"}]
def second = [{"country":"X","code":"XY","cityName":"XYZN","city":"XYZ","timezone":"XYZT","latitude":57.0928,"name":"XYZN","longitude":9.8492}, {"country":"A","code":"AB","cityName":"ABCN","city":"ABC","timezone":"ABCT","latitude":1.234,"name":"ABCN","longitude":29.8482}]
i want to compare these two, but its failing because the first json has longitude and latitude as string while second json has them as numbers.
Also, i dont want to change second json and has to be used as it is.
Please suggest how can i change the type from string to number in first?
I tried https://github.com/intuit/karate#floats-and-integers
but it didnt work out for object array.
Sample Code:
Feature:
Scenario:
* def first = [{"country":"X","code":"XY","cityName":"XYZN","city":"XYZ","timezone":"XYZT","latitude":"57.0928","name":"XYZN","longitude":"9.8492"}, {"country":"A","code":"AB","cityName":"ABCN","city":"ABC","timezone":"ABCT","latitude":"1.234","name":"ABCN","longitude":"29.8482"}]
* def second = [{"country":"X","code":"XY","cityName":"XYZN","city":"XYZ","timezone":"XYZT","latitude":57.0928,"name":"XYZN","longitude":9.8492}, {"country":"A","code":"AB","cityName":"ABCN","city":"ABC","timezone":"ABCT","latitude":1.234,"name":"ABCN","longitude":29.8482}]
* def first_formatted = []
* def fun = function(x){x.latitude = Number(x.latitude); x.longitude = Number(x.longitude); karate.appendTo(first_formatted, x); }
* karate.forEach(first, fun)
* print first_formatted
* match first_formatted == second