How to push new json element to all array elements

Hi,

Below json we are getting in response, and to this api we need to add new element for all input parameter array and send new request to server.

How to push “inputList”:[x] element below all array elements of inputParameters?

Response:
{
“canBeSubscribed”:false,
“inputParameters”:[
{
“allowBlank”:false,
“canBeShownOnUi”:false,
“uiControl”:“1”
},
{
“allowBlank”:false,
“uiControl”:“1”
}
],
“reportTitle”:“Report”
}

New Request:

{
“canBeSubscribed”:false,
“inputParameters”:[
{
“allowBlank”:false,
“canBeShownOnUi”:false,
“uiControl”:“1”,
“inputList”:[

     ]
  },
  {
     "allowBlank":false,
     "uiControl":"1",
	 "inputList":[
        
     ]
  }

],
“reportTitle”:“Report_New”
}

Thanks,
Huligesh

@huligesh.hanumanthap HI!

You can try using a regular expression and add the desired value to the response. Example:

export default function (data) {

 var string = "some string containing var userId = 117051";
  var foundId = string.match(/.+/);

  foundId += ' add string'
  foundId = foundId.concat(' add second string')
  console.log(foundId); 
}

Output:
INFO[0000] some string containing var userId = 117051 add string add second string source=console

Hi @huligesh.hanumanthap !

Adding to the @PaulM 's response, you can use the following approach:

import http from 'k6/http';

export default function () {
  const res = http.get('https://test-api.k6.io/public/crocodiles/');

  let obj = res.json(); 
  console.log(JSON.stringify(obj));

  for (let index = 0; index < obj.length; index++) {
     obj[index]["LOREM"] = [];     
  }

  console.log(JSON.stringify(obj));
}

The output will look like this:

INFO[0001] [{"date_of_birth":"2010-06-27","age":11,"id":1,"name":"Bert","sex":"M"},{"name":"Ed","sex":"M","date_of_birth":"1995-02-27","age":27,"id":2},{"age":37,"id":3,"name":"Lyle the Crocodile","sex":"M","date_of_birth":"1985-03-03"},{"date_of_birth":"1993-12-25","age":28,"id":4,"name":"Solomon","sex":"M"},{"id":5,"name":"The gharial","sex":"F","date_of_birth":"2004-06-28","age":17},{"name":"Sang Buaya","sex":"F","date_of_birth":"2006-01-28","age":16,"id":6},{"date_of_birth":"1854-09-02","age":167,"id":7,"name":"Sobek","sex":"F"},{"sex":"M","date_of_birth":"1981-01-03","age":41,"id":8,"name":"Curious George"}]  source=console
INFO[0001] [{"LOREM":[],"id":1,"name":"Bert","sex":"M","date_of_birth":"2010-06-27","age":11},{"sex":"M","date_of_birth":"1995-02-27","age":27,"LOREM":[],"id":2,"name":"Ed"},{"id":3,"name":"Lyle the Crocodile","sex":"M","date_of_birth":"1985-03-03","age":37,"LOREM":[]},{"name":"Solomon","sex":"M","date_of_birth":"1993-12-25","age":28,"LOREM":[],"id":4},{"id":5,"name":"The gharial","sex":"F","date_of_birth":"2004-06-28","age":17,"LOREM":[]},{"name":"Sang Buaya","sex":"F","date_of_birth":"2006-01-28","age":16,"LOREM":[],"id":6},{"sex":"F","date_of_birth":"1854-09-02","age":167,"LOREM":[],"id":7,"name":"Sobek"},{"sex":"M","date_of_birth":"1981-01-03","age":41,"LOREM":[],"id":8,"name":"Curious George"}]  source=console

Hope that helps,
Cheers