How to update value of a request body json element?

My question:
I am reading data from a csv file. The date value stored in the csv file is like '2023-05-01". But when I dynamically bind that value in the postman body something like
{
appDate:{{APPDATE}}
}
It is converting it as
{
appDate: 2023-05-01
}

What I am looking for is
{
appDate: “2023-05-01”
}

I’ve already tried:
I already tried
{
appDate: “{{APPDATE}}”
}

It should be…

{
    "appDate": "{{APPDATE}}"
}

You do need the quotes around the variable\value. You also need the quotes around the key.

Just tested this, and if you don’t put the quotes, then it will send it without. I thought it might be because its a date but this doesn’t matter. Date is not a valid data type for JSON. It has to be a string. If you enter text instead of a date in the CSV file, it will also try and send it without the quotes (which again isn’t valid JSON).

I tried
{
“appDate”: “{{APPDATE}}”
}

But it is not accepting it and throwing an error “The JSON value could not be converted to System.Nullable`1[System.DateTime]”. When I change the value in the CSV file from 2023-05-01 to “”“2023-05-01"”", it is working. Is it possible to fix this in the code as the test data is being generated through automation & can not be updated as it contains thousands of records?

Where are you getting that error. In Postman or in the response?

Have you checked the console log to see what was sent?

Is the body showing the quotes as you would expect?

It appears to work for me using Postman Echo.

Request Body

image

Console Log

image

It looks formatted correctly to me.

You might be able to create a pre-request script that updates the body as a workaround.

For example…

const body = JSON.parse(pm.request.body.raw);
body.appDate = data.APPDATE;
// you can reference the information in the current iteration by the data object.
pm.request.body.raw = body;
console.log(pm.request.body.raw);

I originally had the following but all this did is put an extra set of quotes around the value.

body.appDate = JSON.stringify(data.APPDATE);

You have two choices here, you can either define the entire body in the pre-request script or retrieve the current body like in the example above.

If you do pull the current body. It has to be valid JSON. You can’t have a variable that is not in a string, as that would not be valid JSON at runtime (as it hasn’t replaced the variable with a value yet) and the JSON.parse(pm.request.body.raw) line will fail instead.

Therefore you can do something like.

{
    "appDate": ""
}

Your probably better off defining the whole body in the pre-request script and leaving it blank in the body of the request. (Still need to set it to JSON\RAW though).

Although I’m not sure this is needed, as all this does is set the body with single quotes.

Can you post a raw copy of the first couple of lines of the CSV file using Notepad (not Excel).
So I can see if it has any quotes around it or not that might mess up the formatting\import.

This is what my test CSV looks like (I also wanted to test a text field and an integer, so added a few columns).

image