How to remove 'null' keyword from field value from Payload?

vishaljaiswal
Giga Expert

Scenario: I am creating an action for payload for integration purpose. In the scripting part i am defining field structure including non mandatory fields as well.

Issue: I dont want to pass 'null' keyword in payload request if field doesn't have any value.

Expected solution: If a field doesn't have any value then simply it show pass like <"">

Sample code which i am using:

var poLineObj = {
    'PurchaseOrderItemText': '',
    'Plant': '1710',
    'OrderItemCategory': '',
    'PriceAmount': '',
}
Incorrect Payload Request which i am getting:
{
    "PO Header": {
        "PurchaseOrderItemText": null,
        "Plant": 1710,
        "OrderItemCategory": null,
"PriceAmount": null

}
}

 

Expected Solution:

 

{
    "PO Header": {
        "PurchaseOrderItemText": "",
        "Plant": 1710,
        "OrderItemCategory": "",
        "PriceAmount": "",

}
}



}

3 REPLIES 3

Anamika Gupta
Tera Contributor
Try this once.
 
var poLineObj = {
    'PurchaseOrderItemText': "',
    'Plant''1710',
    'OrderItemCategory': "'',
    'PriceAmount': "'',
}
    

Robbie
Kilo Patron
Kilo Patron

Hi @vishaljaiswal,

 

To confirm, you're receiving the request with null values. Painful. Ideally, this would be fixed at source, but let's go back to reality and handle it ourselves.

In the past I've replaced the null values similar to the below:

 

If you're handling the JSON object, then you can use this:

data = JSON.stringify(data).replace(/null/g, '""');  //Turns to string

 

 If you're handling it as a string, you can use this:

data = data.replace(/null/g, '""');  //Stays as string

Or:

data = JSON.parse(data);    // Turns to a JSON object

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Helpful.

 

Thanks, Robbie

Robbie
Kilo Patron
Kilo Patron

Hi @vishaljaiswal,

 

Did you see my earlier response?

In the past I've replaced the null values similar to the below:

 

If you're handling the JSON object, then you can use this:

data = JSON.stringify(data).replace(/null/g, '""');  //Turns to string

 

 If you're handling it as a string, you can use this:

data = data.replace(/null/g, '""');  //Stays as string

Or:

data = JSON.parse(data);    // Turns to a JSON object

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Helpful.

 

Thanks, Robbie