Read content from file in raw json request body

I want to test my server via sending raw json data. One of the values of my request body is a really big object, so it would be very helpful if I can read it from a json file instead of pasting a really big raw json string. Is it possible?

I don’ want to read the entire request body from a json file. Instead, I just want to read for one of the values. Thanks.

    "key1": "value1",  // a normal value
    "key2": {...} // a very long object. I want to read this from file

@emrehidden Welcome to the community :partying_face:

Yes you can do that using data driven method. To know more about it please read.

Say this is my example below:

here in the body I just need to replace the email field alone. So I have added curly braces and just named it as email. The name should be given to the attribute in the file as well. You don’t need to define anything more here. Postman is smart enough to read the variables names and match them with the input file provided.

Here I would just need to replace my email alone, and password remains the same.

The data file can either look like:

Only the fields needed,

or the entire file:

In both the cases “email” is mandatory in the file because that’s the field you are referring to in your body.

So in your case,

    "key1": "value1",  // a normal value
    "key2": "{{key2}}" // a very long object. I want to read this from file

you can access the key2 from the JSON file. If the field name is different in the JSON file, pass the field name from which you need to copy the value and pass it under key2.

Hope this is useful :blush:
Let me know if you have any other questions.

Hello @bpricilla , thank you so much for your reply :slight_smile:. So this is really helpful but I am a bit confused. I need to the email field in the request get its content from an entire json file which is not starting with an array ([]).

What I have tried is in the runner, I read the file as data but I gave the error “json file should be an array”. Okay, I have converted it to an array which has one object in it. Then in the pre-request script, I tried to get the data with pm.environment.set("data", data[0]);. Then in the body, I tried to pass it via

"data": {{data}}

But it didn’t helped, when I run the runner it shows that data becomes null in the request body.

Sorry if I’m pushing too much :slight_smile:

1 Like

Hi @emrehidden,

The below code as mentioned will set the data not fetch data.

pm.environment.set("data", data[0]);

If your JSON file contains data as below.


Then when you run the collection it will populate data to request body variable.

"data": {{data}}

Hope this helps :slightly_smiling_face:

1 Like

@emrehidden Okay, can you please share the screenshots if possible?

also if converting to a array is bit tough for you every time, you can opt for a csv file as well. For Data driven approach Postman supports, both CSV and JSON and the process is same :blush:

Also when the field/attribute name is same as your declaration inside the body you don’t need to specifically declare it as a environment/collection/global variable. You can directly read the name in your script.

As @darkphoenix mentioned you can use the variable name, inside double quotes to be in JSON format.

"data": "{{data}}"

Please try this and let us know know if you are facing issues still. Kindly share the screenshots to understand better :slightly_smiling_face: