Karate official document mentions that it returns null if variable/jsonpath is not found. The default value can be overriden using second argument.
When I tried this then for the absent variable I see it is giving null and able to override null. But the same did not go as expected for jsonpath expression. It gives #notpresent which is not overridden by the second argument.
JSON Document
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
// Working example with variables
* def df = 12
* def v741 = karate.get("df", "100")
* print "v741", v741 // output = 12 bcz variable df exists
* v741 = karate.get("df1", "100")
* print "v741", v741 // output = 100 bcz variable df1 does not exists
// non-working example with jsonpath
* def v74 = karate.get("$jsonObject.store.book[1].author1", 100)
* print "v74", v74 // Output = #notpresent
More Update on the issue -
It is certainly a bug. Sorry, I reverted the accepted answer upvote.
* print karate.get("jsonObject.store.book[*].author", 100) // It should give an array of authors but gave 100.
* print karate.get("$jsonObject.store.book[*].author", 100) // It gives an array of authors
You are saying "In other words, use $ only when you want to run JsonPath "searches" or "queries". As per doc, it should return the default value when jsonpath did not return a result.