How to convert integer value to string while in Pre-request script?

I want to post integer column value as string. Can you please help how to convert integer to string in the following example. This is posting last two column (Qty & price) value as integer instead of string.

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

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

Hey @bukhari.adac, :wave:

just change the lines where you are setting the value as following

value: "" + value;

and

requestBody[key] = "" + value;

That would covert the data inside value to a string before storing. Now when you to JSON.stringify() it will take care of adding the quotes around the value, since it’s a string now.

1 Like

Great, it worked. Thanks @amit.

I also want to know how to skip a specific column and its value while using collection runner, like bullet3 in this particular example.