'How to remove extra backslashes from my json result in karate?

I'm reading an xls file in java and I'm converting that xls file data into a Hashmap. I'm returning this hashmap to the feature file. But i'm getting output something like this.

{
 "names": [
   "{\"color\":\"blue\",\"name\":\"sushma\",\"id\":\"1\"}",
   "{\"color\":\"orange\",\"name\":\"divya\",\"id\":\"2\"}",
   "{\"color\":\"pink\",\"name\":\"lohi\",\"id\":\"3\"}"
 ]
}

where names is the sheet name and the remaining is data of the sheet.

* json results = doTesting.readExcel("C:\\Users\\sushma.i.dangeti\\eclipse-workspace\\xls\\ReadingxlsDemo.xls", "names")

Instead of using def, I have used json in the feature file but it is also not working. And also if I'm trying to access for instance, results.names[0].color then Its not printing anything. Can someone help me with this issue. Thanks in advance



Solution 1:[1]

Since you got JSON from an external source, you need to take care of conversions: https://github.com/karatelabs/karate#type-conversion

Here's a tip, this should convert the JSON you provided in your question. Try it and see.

* def raw =
"""
{
 "names": [
   "{\"color\":\"blue\",\"name\":\"sushma\",\"id\":\"1\"}",
   "{\"color\":\"orange\",\"name\":\"divya\",\"id\":\"2\"}",
   "{\"color\":\"pink\",\"name\":\"lohi\",\"id\":\"3\"}"
 ]
}
"""
* def fun = function(x){ return karate.fromString(x) }
* raw.names = karate.map(raw.names, fun)
* print raw

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Thomas