1

I have the following JSON. I want to change the keyName 'freeDelivery' to 'isFreeDelivery' but I can't figure out how to do it.

{
    "result": [
        {
            "deliverySlots": [
                {
                    "id": "2DNN",
                    "date": "2022-04-05",
                    "freeDelivery": false,
                    "label": "All day delivery 08:30am to 5pm",
                    "price": "£5.00",
                    "fullSlotId": "2DNN"
                },
                {
                    "id": "2DPM",
                    "date": "2022-04-05",
                    "freeDelivery": false,
                    "label": "Afternoon 12pm to 5pm",
                    "price": "£10.00",
                    "fullSlotId": "2DPM"
                }
                ]
        },
        {
            "deliverySlots": [
                {
                    "id": "2DNN",
                    "date": "2022-04-06",
                    "freeDelivery": false,
                    "label": "All day delivery 08:30am to 5pm",
                    "price": "£5.00",
                    "fullSlotId": "2DNN"
                },
                {
                    "id": "2DPM",
                    "date": "2022-04-06",
                    "freeDelivery": false,
                    "label": "Afternoon 12pm to 5pm",
                    "price": "£10.00",
                    "fullSlotId": "2DPM"
                }
            ]
        }
    ]
}

I've looked at the following pages but still can't figure out how to do it. Do I have to do a transorm or is there an easier way?

https://github.com/karatelabs/karate/blob/master/karate-junit4/src/test/java/com/intuit/karate/junit4/demos/js-arrays.feature https://github.com/karatelabs/karate#json-transforms

Matt
  • 773
  • 2
  • 15
  • 30

1 Answers1

1

Here you go:

* def payload = { before: 'foo' }
* remove payload.before
* payload.after = 'bar'
* match payload == { after: 'bar' }

Instead of remove this will also work (using pure JS):

* eval delete payload.before

EDIT: after seeing the comments, I would treat this as a JSON transform.

* def payload = { before: 'foo' }
* def fun = function(x){ var res = {}; res.after = x.before; return res }
* def result = fun(payload)
* match result == { after: 'foo' }

I'm sure now you'll want to "retain" all the existing data. Fine, here you go:

* def payload = { before: 'foo' }
* def fun = function(x){ var res = x; res.after = x.before; delete res.before; return res }
* def result = fun(payload)
* match result == { after: 'foo' }

And you already know that you can run a transform on all array elements like this:

* def result = karate.map(someArray, fun)

Please note that you can create 2 or 3 transforms - and "nest" them.

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Thanks Peter but I don't want to remove the key I just want to change the key name but keep the value. Also there are many occurences of 'freeDelivery' keys and I need to change them all. I've added more of the JSON in the question so it makes more sense. – Matt Apr 01 '22 at 15:24
  • @Matt see my edit. beyond this if you want simplification, I have only this to say: https://stackoverflow.com/a/54126724/143475 – Peter Thomas Apr 01 '22 at 16:29
  • Thanks again @peter. I've managed to change the first occurrence of 'id' by manually entering the json path as shown below...but how can I change all of them? Do I have to loop through them all? Is there an easier way of finding the key without manually entering the full JSON Path? * def jsonTransformFunction = function(passedInJson){ var newJson = passedInJson; newJson.result[0].deliverySlots[0].changedId = passedInJson.result[0].deliverySlots[0].id; delete newJson.result[0].deliverySlots[0].id; return newJson } – Matt Apr 04 '22 at 19:03
  • @Matt when I say a `transform on all array elements` that *is* a loop. otherwise you are expecting Karate to be a programming language. I have nothing more to add to all the hints that I have provided. – Peter Thomas Apr 05 '22 at 05:18