How to skip blank array field in POST or PUT request

In a post or put request, I want to skip field in array when variable is blank in CSV file. Here in this example “bullets5” is blank.

fields in CSV file
item_id,title,bullets1,bullets2,bullets3,bullets4,bullets5
1234,men shoes,Textile,Rubber sole,Lace up closure,fabric lining,

{
“item_id”: “{{item_id}}”,
“fields”: [
{ “name”: “title”, “value”: “{{title}}”},
{ “name”: “bullets1”, “value”: “{{bullets1}}”},
{ “name”: “bullets2”, “value”: “{{bullets2}}”},
{ “name”: “bullets3”, “value”: “{{bullets3}}”},
{ “name”: “bullets4”, “value”: “{{bullets4}}”},
{ “name”: “bullets5”, “value”: “{{bullets5}}”}
]
}

Thanks.

Hey, @bukhari.adac!

To do this, you can alter the request body in a pre-request script. Change the body of the request to equal just the variable {{requestBody}}. This will be a local variable.

Then add something like this to the pre-request script:

const requestBody = {
    item_id: null,
    fields: []
};

for (let [key, value] of Object.entries(data)) {
    if (key.startsWith('bullets') || key === 'title') {
        if (value !== '') {
            requestBody.fields.push({
                name: key,
                value: value
            });
        }
    } else {
        requestBody[key] = value;
    }
}

pm.variables.set('requestBody', JSON.stringify(requestBody));

Now when you execute the script in the Collection Runner, the shape of the request body should match what you expect. You can verify this by looking at the logs in the Postman Console.

Hope this helps!

Best,

Kevin

1 Like

Hi Kevin,
Thank you for response. I have copied your suggested script under pre-request script. Can you please let me know what I have to change in the body?

Body:

{
“item_id”: “{{item_id}}”,
“fields”: [
{ “name”: “title”, “value”: “{{title}}”},
{ “name”: “bullets1”, “value”: “{{bullets1}}”},
{ “name”: “bullets2”, “value”: “{{bullets2}}”},
{ “name”: “bullets3”, “value”: “{{bullets3}}”},
{ “name”: “bullets4”, “value”: “{{bullets4}}”},
{ “name”: “bullets5”, “value”: “{{bullets5}}”}
]
}

Thanks.
Adac

as mentioned by kevin. In Body change the body of request with Variables {{variablename}} local one.

It worked. Thanks Kevin & gpub1.

Another question. Using same set of example with additional column contain integer value, I want to post integer column value as string. Can you please help how to convert integer to string in following example:

columns in CSV file
item_id,title,bullets1,bullets2,bullets3,bullets4,bullets5,qty,price
1234,men shoes,Textile,Rubber sole,Lace up closure,fabric lining, ,989,10

Body:

{
“item_id”: “{{item_id}}”,
“fields”: [
{ “name”: “title”, “value”: “{{title}}”},
{ “name”: “bullets1”, “value”: “{{bullets1}}”},
{ “name”: “bullets2”, “value”: “{{bullets2}}”},
{ “name”: “bullets3”, “value”: “{{bullets3}}”},
{ “name”: “bullets4”, “value”: “{{bullets4}}”},
{ “name”: “bullets5”, “value”: “{{bullets5}}”}
{ “name”: “qty”, “value”: “{{qty}}”},
{ “name”: “price”, “value”: “{{price}}”}
]
}

Pre-request script:

    const requestBody = {
        item_id: null,
        fields: []
    };

    for (let [key, value] of Object.entries(data)) {
        if (key.startsWith('bullets') || key === 'title') {
            if (value !== '') {
                requestBody.fields.push({
                    name: key,
                    value: value
                });
            }
        } else {
            requestBody[key] = value;
        }
    }

    pm.variables.set('requestBody', JSON.stringify(requestBody));

Thanks.

Adac

Hi! You can just do it via this code:

Just paste it in your prerequest script :slight_smile: