Validation of each field in JSON response using karate API

Viewed 31

I want to validate particular fields in the response whether it is integer or float(ex: fullbathrooms field). I tried below code but getting match failed error. Could you please help here ?.....Thanks

Given path '/property-client'
And request {"address": <address>,"city": <city>,"state": <state>,"zipCode": <zipCode>}
When method post
Then status 200
And print response

And match response == {fullbathrooms:'#number'}

Examples: |read('testFile1.csv')|

Error : match failed: EQUALS

Actual response:

{ "success": true, "message": { "version": "1.0", "response": { "id": "94568859", "type": "express", "responseheader": null, "reportdata": { "property": { "source": null, "type": null, "dom": null, "propertytype": "Single Family Residence", "standardtype": null, "address": { "documentid": null, "number": "150", "directional": null, "street": "BRIDGE", "suffix": "RD", "postdirectional": null, "unit": "", "city": "HILLSBOROUGH", "state": "CA", "zip": "94010", "zipplus4": "6908", "fulladdress": "150 BRIDGE RD, HILLSBOROUGH, CA 94010" }, "info": { "type": null, "fips": "6081", "county": "San Mateo", "bedrooms": "5", "bathrooms": "6.50", "fullbathrooms": "6.50", "totalrooms": "0", "livingarea": "7750", "totallivingarea": "7750", "landarea": "41382", "landareatype": null, "pool": "true", "landvalue": "6904800", "improvementvalue": "3284414", "assessedvalue": "10189214", "assessedyear": "2021", "taxvalue": "11746898", "taxyear": "2021", "deliquentyear": null, "yearbuilt": "2011", "propertytax": null, "approxage": "11", "parcelnumber": "032-400-110", "titlecompany": null, "geocode": { "latitude": "37.563272", "longitude": "-122.334442", "geoqualitycode": "" } }

1 Answers

Please take some time to read the documentation: https://github.com/karatelabs/karate#match-contains

I'm not going to refer to your response dump (which by the way is not valid JSON), but give you a simple example. Please pay attention to the structure of your JSON. And note that the 6.50 is a string not a number in your response.

* def response = { "foo": { "bar": { "fullbathrooms": "6.50" } } }
* match response.foo.bar == { fullbathrooms: '#string' }

If you want to validate numbers within strings, please refer other answers, for example: https://stackoverflow.com/search?q=%5Bkarate%5D+number+regex

Related