# How to skip blank array field in POST or PUT request

**URL:** <https://community.postman.com/t/how-to-skip-blank-array-field-in-post-or-put-request/12403>\
**Category:** Help Hub\
**Tags:** variables, collections\
**Created:** [May 6, 2020, 1:51am UTC](https://community.postman.com/t/how-to-skip-blank-array-field-in-post-or-put-request/12403 "2020-05-06T01:51:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bukhari.adac](https://avatars.discourse-cdn.com/v4/letter/b/c2a13f/32.png) [@bukhari.adac](https://community.postman.com/u/bukhari.adac)\
**Post date:** [May 6, 2020, 1:51am UTC](https://community.postman.com/t/how-to-skip-blank-array-field-in-post-or-put-request/12403/1 "2020-05-06T01:51:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kevin.swiber](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kevin.swiber/32/3820_2.png) [@kevin.swiber](https://community.postman.com/u/kevin.swiber)\
**Post date:** [May 7, 2020, 4:31am UTC](https://community.postman.com/t/how-to-skip-blank-array-field-in-post-or-put-request/12403/2 "2020-05-07T04:31:02Z")

</div>

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:

```auto
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

---

<div class="post-metadata">

**Author:** ![bukhari.adac](https://avatars.discourse-cdn.com/v4/letter/b/c2a13f/32.png) [@bukhari.adac](https://community.postman.com/u/bukhari.adac)\
**Post date:** [September 3, 2020, 12:35am UTC](https://community.postman.com/t/how-to-skip-blank-array-field-in-post-or-put-request/12403/4 "2020-09-03T00:35:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gpub1](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@gpub1](https://community.postman.com/u/gpub1)\
**Post date:** [September 3, 2020, 7:33pm UTC](https://community.postman.com/t/how-to-skip-blank-array-field-in-post-or-put-request/12403/5 "2020-09-03T19:33:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bukhari.adac](https://avatars.discourse-cdn.com/v4/letter/b/c2a13f/32.png) [@bukhari.adac](https://community.postman.com/u/bukhari.adac)\
**Post date:** [September 25, 2020, 2:32am UTC](https://community.postman.com/t/how-to-skip-blank-array-field-in-post-or-put-request/12403/7 "2020-09-25T02:32:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mondonno](https://avatars.discourse-cdn.com/v4/letter/m/aca169/32.png) [@Mondonno](https://community.postman.com/u/Mondonno)\
**Post date:** [January 27, 2021, 5:53pm UTC](https://community.postman.com/t/how-to-skip-blank-array-field-in-post-or-put-request/12403/8 "2021-01-27T17:53:40Z")

</div>

Hi! You can just do it via this code:

> <https://gist.github.com/Mondonno/182e12880d609374cd41ea64d054f225>

Just paste it in your prerequest script 🙂
