1

Here is what I need to build dynamically

{
    "recommendedAssociations": [
        {
            "StudentId": "PIP21|23455",
            "SchoolID": "23331",
            "Status": "ACCEPT"
        },
        {
            "StudentId": "DLO14|44445",
            "SchoolId": "23331",
            "feedbackStatus": "ACCEPT"
        },
        {
            "StudentId": "DEF66|43432",
            "SchoolId": "2331",
            "feedbackStatus": "ACCEPT"
        },
        {
            "StudentId": "334|11123",
            "customerOrganizationIdentifier": "2331",
            "feedbackStatus": "REJECT"
        }
    ],
    "recommendationMsg": "Student Acceptance"
}  

How can I create the items dynamically?

Wai Ha Lee
  • 8,173
  • 68
  • 59
  • 86

1 Answers1

1

If you know JavaScript - you can use it directly to "edit" any JSON.

For example:

* def data = {}
* data.foo = 'bar'
* match data == { foo: 'bar' }

And arrays are easy:

* def data = []
* data[0] = { foo: 'bar' }
* match data == [{ foo: 'bar' }]

Here's another example: https://stackoverflow.com/a/69041614/143475

For more ideas, read the documentation: https://github.com/intuit/karate#json-transforms

Peter Thomas
  • 47,282
  • 14
  • 68
  • 196
  • Thanks. However, I need a way do dynamically create arrays, I should be able dynamically pass the number of elements in a json array – Alexander the Great Sep 12 '21 at 23:11
  • @AlexandertheGreat in my opinion that is answered in the link in my answer. and you can always call `push()` on an array. if that doesn't help, maybe you should look for some other option. note that your question is not clear at all. – Peter Thomas Sep 13 '21 at 06:08
  • Sorry for not a clear question. What I am trying to do is dynamically build a json array, which has to be dynamic length. – Alexander the Great Sep 13 '21 at 15:49
  • @AlexandertheGreat I'll try one last time. you can call `karate.appendTo()` on an array any number of times to add items to it - here is an example: https://stackoverflow.com/a/62567412/143475 - if that doesn't answer your question, it means that Karate does not support what you want - and you should look for some other tool – Peter Thomas Sep 13 '21 at 16:51