Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Karate - How to import json data

I want to import some JSON data to my tests.
In order to documentation I should do that like this:

* def data = read('classpath:init/data.json')

I've created my JSON file with this content:

{
    "name": "ehsan"
}

This is my code:

  Background:
    * def data = call read('classpath:init/data.json')

  Scenario:
    * print data

But it prints nothing and says:

16:11:30.898 [main] WARN com.intuit.karate - not a js function or feature file: read('classpath:init/data.json') - [type: JSON, value: com.jayway.jsonpath.internal.JsonContext@7d61eccf]
like image 878
ehsan shirzadi Avatar asked Sep 20 '25 08:09

ehsan shirzadi


2 Answers

Below code is correct:

* def data = read('classpath:init/data.json')

Only you must remove [call]

like image 142
Homayoun Behzadian Avatar answered Sep 22 '25 23:09

Homayoun Behzadian


Yes, read the error message (and the doc) carefully - there is no meaning in 'calling' a JSON file, the moment you read it - you have your re-usable data already. Just do this:

Background:
    * def data = read('classpath:init/data.json')

  Scenario:
    * print data
like image 26
Peter Thomas Avatar answered Sep 22 '25 22:09

Peter Thomas