Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to push the json objects to array using karate object

Question is related to adding json objects to array using karate API
Below is my code.

    * def appendBasics =
    """
    function() {
    var group = {}
group['details'] = group['details'] || []
     karate.append(group['details'], { b: 2 })
     karate.append(group['details'], { b: 4 })
     karate.log(group)
    }
    """
    * def bar = appendBasics()
    * print bar

Output which I am expecting is

{
  "details": [
    {
      "b": "2"
    },
    {
      "b": "4"
    }
  ]
}

Output which I am getting is

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
  "details": [
  ]
}

Could you please suggest.

>Solution :

Just use native array operations. The use of karate.append() etc and even karate.map() is not needed any more. I revised your code below:

* def appendBasics =
"""
function() {
  var group = {}
  group['details'] = group['details'] || []
  group['details'].push({ b: 2 });
  group['details'].push({ b: 4 });
  return group;
}
"""
* def bar = appendBasics()
* print bar

Refer: https://stackoverflow.com/a/76091034/143475

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading