How to prevent serialization of data when using the runner?

Hi there!

I want to loop over a JSON array of objects and use each object directly as body, and send it as it is.

I upload my JSON fil to the runner and add the request to execute. The runner identifies the number of iterateion correctly. I set my request’s body to raw and JSON and clear it. Then a add this pre-script:

let currentData = pm.iterationData.toJSON();
console.log("Sending request body:", JSON.stringify(currentData));
pm.request.body.raw = JSON.stringify(currentData);

But when I start a run, instead of sending my data, Postman send its serialized version.

How to prevent this behaviour and send just the data from the JSON file?

My data:

[
    {
        "title": "Project A Title",
        "role": "Project A Role",
        "from": "1970-01",
        "to": "1970-12",
        "tasks": "Project A Tasks",
        "tools": null,
        "skills": [
            "Java",
            "Python",
            "PHP"
        ],
        "customerName": "Project A Customer",
        "displayCustomerName": true,
        "location": null,
        "businessDepartment": null,
        "businessBranch": null,
        "contractType": null,
        "contractHoursPercentage": null,
        "hourlyRate": {
            "paymentRate": null,
            "paymentCurrency": "EUR"
        },
        "selectedDirektProject": null
    }
]

What Postman sends:

  {"id":"33252f12-80ca-4ab1-a5b8-209de4cc501c","values":[{"type":"any","value":"Project A Title","key":"title"},{"type":"any","value":"Project A Role","key":"role"},{"type":"any","value":"1970-01","key":"from"},{"type":"any","value":"1970-12","key":"to"},{"type":"any","value":"Project A Tasks","key":"tasks"},{"type":"any","value":null,"key":"tools"},{"type":"any","value":["Java","Python","PHP"],"key":"skills"},{"type":"any","value":"Project A Customer","key":"customerName"},{"type":"any","value":true,"key":"displayCustomerName"},{"type":"any","value":null,"key":"location"},{"type":"any","value":null,"key":"businessDepartment"},{"type":"any","value":null,"key":"businessBranch"},{"type":"any","value":null,"key":"contractType"},{"type":"any","value":null,"key":"contractHoursPercentage"},{"type":"any","value":{"paymentRate":null,"paymentCurrency":"EUR"},"key":"hourlyRate"},{"type":"any","value":null,"key":"selectedDirektProject"}]}

Hey @automatix :wave:

Welcome to the Postman Community! :postman:

If you have your JSON file structured like this, using the body as the key:

[
	{
		"body": {
			"title": "Project A Title",
			"role": "Project A Role",
			"from": "1970-01",
			"to": "1970-12",
			"tasks": "Project A Tasks",
			"tools": null,
			"skills": [
				"Java",
				"Python",
				"PHP"
			],
			"customerName": "Project A Customer",
			"displayCustomerName": true,
			"location": null,
			"businessDepartment": null,
			"businessBranch": null,
			"contractType": null,
			"contractHoursPercentage": null,
			"hourlyRate": {
				"paymentRate": null,
				"paymentCurrency": "EUR"
			},
			"selectedDirektProject": null
		}
	},
    {
        "body": {
			"title": "Project B Title",
			"role": "Project B Role",
			"from": "1970-01",
			"to": "1970-12",
			"tasks": "Project B Tasks",
			"tools": null,
			"skills": [
				"Java",
				"Python",
				"PHP"
			],
			"customerName": "Project B Customer",
			"displayCustomerName": true,
			"location": null,
			"businessDepartment": null,
			"businessBranch": null,
			"contractType": null,
			"contractHoursPercentage": null,
			"hourlyRate": {
				"paymentRate": null,
				"paymentCurrency": "EUR"
			},
			"selectedDirektProject": null
		}
    }  
]

You could add something basic to the pre-request script that sets the value of payload at runtime and takes the body variable from the JSON datafile:

When the request body contains {{payload}} it will use the current iteration data as the request body.

Is that what you’re trying to do?

I think you just need to change that to toObject().

let currentData = pm.iterationData.toObject();
console.log("Sending request body:", JSON.stringify(currentData));
pm.request.body.raw = JSON.stringify(currentData);

Thank you! Just tried this out. The console log looks good. But Postman sends the request with a empty body. that mean pm.request.body.raw doesn’t get replaced.

I tested this using Postman Echo.

With this JSON as the data file.

[
    {
        "title": "Project A Title",
        "role": "Project A Role",
        "from": "1970-01",
        "to": "1970-12",
        "tasks": "Project A Tasks",
        "tools": null,
        "skills": [
            "Java",
            "Python",
            "PHP"
        ],
        "customerName": "Project A Customer",
        "displayCustomerName": true,
        "location": null,
        "businessDepartment": null,
        "businessBranch": null,
        "contractType": null,
        "contractHoursPercentage": null,
        "hourlyRate": {
            "paymentRate": null,
            "paymentCurrency": "EUR"
        },
        "selectedDirektProject": null
    }
]

The console logs shows the body the request was sent with.

It is also echoed back from the Postman Echo API. Your request gets echoed back with the data element.

image

Can you confirm that your body is set to raw\JSON?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.