How to run newman on a list of files containing request body

I read the JSON file which is an array of objects. I aim to put all these inside a key called “jsonContents” and wrap this new object inside an array (which would be of length 1). My problem is that the following line doesn’t make an array:

$newJsonObject = @(
        @{"jsonContents" = $jsonObject}
    )

The output is still like the following:

{
    "jsonContents": [<Original JSON>]
}

Notice how there are no square brackets. I’ve tried a few things, but I’m having trouble figuring it out since my PowerShell knowledge is incomplete.

I’ve had a quick play with PowerShell, but haven’t spent too much time on this.

Based on the following content file. (testFile.json)

[
	{
		"id": null,
		"geohash": null,
		"latitude": 19.26362,
		"sequence": 0,
		"longitude": 73.07356,
		"googleAddress": null
	},
	{
		"id": null,
		"geohash": null,
		"latitude": 19.26045,
		"sequence": 1,
		"longitude": 73.06872,
		"googleAddress": null
	}
]

The following uses the ConvertTo-Json -compress option to minify the JSON and removing any line breaks, etc.

$importFile = Get-Content -Raw testFile.JSON 

$json = (ConvertFrom-Json $importFile) | ConvertTo-Json -Compress

$body = @(
    @{jsonObject = $json }
)

ConvertTo-Json -InputObject $body

The above is nearly there, but not exactly how I would want it.

It creates the array, with the single object in it.

But the $json object is treated as a string instead of the array.

[
    {
        "jsonObject":  "[{\"id\":null,\"geohash\":null,\"latitude\":19.26362,\"sequence\":0,\"longitude\":73.07356,\"googleAddress\":null},{\"id\":null,\"geohash\":null,\"latitude\":19.26045,\"sequence\":1,\"longitude\":73.06872,\"googleAddress\":null}]"
    }
]

For your use case, this might be ok as you can then parse the string in the pre-request script in Postman.

You might want to pose a question on Stack Overflow or on the Microsoft forums as this is more of a PowerShell query at this point in time.

Had a bit more of a play, and this seems to work.

$importFile = Get-Content -Raw testFile.JSON 

$body = @"
[ 
  "jsonObject": $($importFile) 
]
"@


$body | Set-Content updatedFile.json

This returns the following.

[ 
  "jsonObject": [
	{
		"id": null,
		"geohash": null,
		"latitude": 19.26362,
		"sequence": 0,
		"longitude": 73.07356,
		"googleAddress": null
	},
	{
		"id": null,
		"geohash": null,
		"latitude": 19.26045,
		"sequence": 1,
		"longitude": 73.06872,
		"googleAddress": null
	}
] 
]

Ok the second to last square bracket is out of place, but that is something that I can accept. :slight_smile:

1 Like

Only a small correction is to add curly brackets to ensure the correct JSON format:

$body = @"
[{ 
  "jsonObject": $($importFile) 
}]
"@

And here’s my pre-request body for anyone interested:

pm.request.body.raw = pm.variables.get("jsonObject");

Thanks @michaelderekjones for the help!

1 Like

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