MKod
MKod

Reputation: 823

Karate Framework - How to inject array to a JSON key in the payload?

I am trying to inject array (values) to a json key propertyBatch, and this key will have multiple array elements as values, see below -payload ex:

{
  "fname": "value1",
  "propertyBatch":
    [ 
      {
      "propId": 11,
      "subPropId": {
                    "id": [
                            "4234"
                          ]
                   }
      },
      {
      "propId": 12,
      "subPropId": {
                    "id": [
                           "4235"
                          ]
                   }
      }
    ]
}     

In a scenario, I will have a payload like the above and then I like to inject json to the -propertyBatch. To be more specific, Assume that in first occasion it may have propertyBatch of length 1, meaning it has one json value, and in second run -propertyBatch might have 2 or 3 values and the length can be 2 or 3 based on number of json values injection. As shown in above payload, I have propertyBatch with length 2 because it has two json values.

I want to achieve this, and I thinking my scenario be Scenario Outline: with Examples , and examples will have json values to replace or inject key - propertyBatch

Note, I tried various examples and variety of approaches that are explained in the following links. And also implemented java code using javax.Json library, but the return value is a String or array, and I am not able to succeed or solve my objective.

Karate

stackoverflow

Upvotes: 1

Views: 761

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

Embedded expressions can be used:

* def baz = [1, 2]
* def foo = { bar: 1, baz: '#(baz)' }
* match foo == { bar: 1, baz: [1, 2] }

* def baz = [3]
* def foo = { bar: 1, baz: '#(baz)' }
* match foo == { bar: 1, baz: [3] }

Upvotes: 1

Related Questions