How to traverse through a Json in Karate

Viewed 87

I am trying to have a Karate test where I need to traverse through a json to get the required test data.

I have the below json.

{
   "dev":{
      "scenario1":{
         "data":"data1"
      },
      "scenario2":{
        "data":"data2"
      }
   },
   "qa":{
         "scenario1":{
         "data":"data1"
      },
      "scenario2":{
        "data":"data2"
      }
   }
}

Below is my feature file.

Background:
* def env = dev

Scenario:

* Given url someurl
* def inputData = testdata.env.scenario1.data
* And request { input: '#(inputData)'}
* When method post
* Then status 200

I need the test data that matches the value env that I have defined above. How can I set the json path to my input data. Basically, the json path should take the value from a variable I have defined previously.

1 Answers
Related