1

I have a scenario that reads a json field from a file and compares it to another json field. When I do the match, it says the field from the file is type "other". Sorry for the screen shots, but I think the print statements are helpful.

This is the file, I am not sure if I can provide an example without using the file because when I set the field in the scenario, it works differently, the value of address is then a string in the match statement. It must have something to do with the data returned from the read row? This is what the data looks like that comes back from the read row: { "address": "{"street":"123 Main Street", "city":"Queens", "state":"NY"}" } file with example in it

Scenario: test error for stack overflow
  * def testAddress = db.readRow("select address from json_issue where person = 'Carole'")
  * print testAddress.address
  * def jsonAddressMatch =
  """
  {
    "street":"123 Main Street",
    "city": "Queens",
    "state": "NY"
  }
  """
  * print jsonAddressMatch
  * match testAddress.address == jsonAddressMatch

This is the first test's results test 1

I tried to see if I could convert it to json thinking it was not being interpreted as json. I added these two lines, but the value is a string so it still is not the correct type.

     * json addressObject = testAddress.address
     * match addressObject.value == jsonAddressMatch 

This is the second test's results using "json" to convert test 2

1 Answers1

0

The db.readRow() is clearly returning something that's NOT a Java Map or List, so if you fix that you are on your way.

Refer the docs: https://github.com/karatelabs/karate#type-conversion

The result of * print testAddress.address clearly shows that it is NOT something Karate could auto-convert to JSON, else the output would have been indented correctly.

Now, in a pinch if you know that the toString() of whatever is being returned gives you valid JSON, you can do this:

* def testAddress = karate.fromString(testAddress)
Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • 1
    Thank you for the quick response. I was able to convert it to string using toString() and then was able to convert it to json using karate.fromString or json type conversion. I also found out that the object being returned from db.readRow was a PGobject. Thank you for the hint about the indents indicating whether or not karate could auto-convert to JSON as well. – Carole Tierney Jun 22 '22 at 20:59