Closed rzizza closed 7 years ago
@rzizza The ConvertFrom-Json utility requires parsing depth to be specified as -Depth
, notice the capitalized D
. Feel free to reopen this issue if you're experiencing difficulties. 😄
Thanks @kunagpal, but this does not solve the issue.
First of all, I assume you mean ConvertTo, not ConvertFrom, since ConvertFrom does not have a -Depth parameter. The issue is that the original JSON saved from the Postman/Newman results is not saving the Response Body as JSON.
To elaborate, after my Newman execution, the JSON report file from Postman is saved such that the response body looks like this:
I want to access the value in (JSON File).executions[1].response.body.results[0].geometry.location, so in Powershell, I have:
However, this result is [null]. If I instead do $wwwhResults[1].response.body, then I will get back the string body seen above. When attempting to convert $wwwhResults[1].response.body to JSON format and then back again to access the values, the value returned is still a single object containing the string body (the Depth is not working on this string, I'm guessing because all the \n's are causing the string to not properly be "in JSON format").
When I use Write-Host on the response body, it looks nice, because the "\n's" are formatting the string nicely. But the actual object of the response body is still a string.
I guess maybe a work-around would be to strip the extraneous characters and spaces from the response body, and reformat before trying to use ConvertTo-JSON.
I was just hoping that Newman could save the JSON Response Body in the proper JSON formatting, like how it is returned in the response from the Postman App.
Let me know if you have any questions.
In that case, a workaround would be to use Newman programmatically, like so:
var newman = require('newman'); // run npm install newman in the same folder as this script
newman.run({
collection: process.env.collection, // equivalent to $collection
reporters: ['json'],
reporter: {
json: {
export: process.env.wwwhReportPath
}
},
iterationData: process.env.wwwhInput,
iterationCount: Number(process.env.iterations),
noColor: true,
disableUnicode: true
}, function (err, summary) {
err && console.error(err);
summary.run.executions.forEach(function (execution) {
// execution.response.json() provides a JSON representation of the response body
});
});
With the above code in a file (script.js, say), you could run your collection like so:
node script.js
Within the forEach block in the above snippet, you could process the JSON response bodies in any desired way.
Additionally, you'll also have to change $collection
, etc in the provided sample, to $env:collection
, so that newman can pick up those values correctly from the environment.
Thank you, this looks like it might work for me. I will let you know when I get around to implementing it!
@rzizza Glad to hear that. I'm closing this issue for now, but feel free to get back 😄
Steps to reproduce the problem:
newman run $collection -r 'cli,json' --no-color --disable-unicode --reporter-json-export $wwwhReportPath -d $wwwhInput -n $iterations | Tee-Object $consoleReportPath
I would really like to be able to use Powershell's ConvertFrom-Json and be able to access the individual key-value pairs of the response, but since the body is a string, I can't do that. Using ConvertTo-Json on this string only seems to convert the string to depth of 1 (and all the deeper levels of the JSON are still a string), despite using -depth 5 while converting to JSON.
Does anyone else have this issue, or a work around?