How to set array data in javascript ?

I am getting data from the backend as shown below:

The gstin number is depends on backend i.e it can be multiple or single or Null.

{
    "result": {
        "gstinList": [
            {
                "gstin": "123456789"
            },
            {
                "gstin": "987654321"
            }
        ]
    }
}

Now i want to extract gstin data and create a new request packet as shown below:

{
    "task": "gstinSearch",
    "essentials": [
        {
            "gstin": "123456789"
        },
        {
            "gstin": "abcdefghi"
        }
    ]
}

The gstin is dynamic. Now I want to create request packet and send to the other route. Can anyone help me how to do this part?

Solved Solved
0 3 541
1 ACCEPTED SOLUTION

Not applicable

ok, to remove the parameter just use the following javascript. Then you don't need the extract variable policy.

var json_body = JSON.parse(context.getVariable("request.content"));



var body1 = json_body.result.gstinList;

for(var x=0; x < body1.length; x++) 
{ 
	delete body1[x].status; 
	delete body1[x].state;
}


body1s = JSON.stringify(body1);

context.setVariable("essentials_value", body1s);

View solution in original post

3 REPLIES 3

Not applicable

You need first one extract variable policy, using this you will get the array. then one assign message policy to frame your request payload.

<ExtractVariables name="ExtractVariables-3">
   <Source>request</Source>
   <JSONPayload>
      <Variable name="essentials_value">
         <JSONPath>$.result.gstinList</JSONPath>
      </Variable>
   </JSONPayload>
</ExtractVariables>

Next is assign message

<AssignMessage name="set-payload-3">
  <Set>
    <Payload contentType="application/json">
      {
	
    	"task": "gstinSearch",
    	"essentials": "{essentials_value}"
      }
    </Payload>
  </Set>
</AssignMessage>

Below is the proper data i am getting from the backend. From this packet I want to extract gstin.

{
    "result": {
        "gstinList": [
            {
                "gstin": "123456789",
                "status": "ACTIVE",
                "state": "KARNATAKA"
            },
            {
                "gstin": "987654321",
                "status": "ACTIVE",
                "state": "MAHARASHTRA"
            }
        ]
    }
}


Above I am getting data. So if I want to set the data shown as above then I have to use javascript but in javascript how to write proper code for this.

Not applicable

ok, to remove the parameter just use the following javascript. Then you don't need the extract variable policy.

var json_body = JSON.parse(context.getVariable("request.content"));



var body1 = json_body.result.gstinList;

for(var x=0; x < body1.length; x++) 
{ 
	delete body1[x].status; 
	delete body1[x].state;
}


body1s = JSON.stringify(body1);

context.setVariable("essentials_value", body1s);