1

I want to export a JsMap (made with karate.merge) as a json file, using karate.write. My problem is that the file only consists of one line:

{"a":1,"b":2}

While I expect something like this (formatted over 4 lines):

{
  "a":1,
  "b":2
}

My code, with all the conversions I tried (which led to the same result):

  Scenario: merge
    * def foo = { a: 1 }
    * def bar = karate.merge(foo, { b: 2 })
    * match bar == { a: 1, b: 2 }
    * karate.write(bar, 'bar1.json')
    * karate.write(bar, 'bar2.txt')
    * karate.write(karate.toJson(bar), 'bar3.json')
    * karate.write(karate.toJson(bar), 'bar4.txt')
    * string bar = bar
    * karate.write(bar, 'bar5.json')
    * karate.write(bar, 'bar6.txt')
    * karate.write(karate.toJson(bar), 'bar7.json')
    * karate.write(karate.toJson(bar), 'bar8.txt')

What would be the correct conversion to achieve my desired result?

1 Answers1

1

This will get you what you want:

* def data = {"a":1,"b":2}
* def file = karate.write(karate.pretty(data), 'test.json')
* print 'saved to:', file

Karate is not really to be seen as a JSON formatting tool, so consider Java interop if needed. And karate.write() has some caveats, refer: https://stackoverflow.com/a/54593057/143475

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248