Save body results to a CSV or JSON file - using newman

I know this is old but this is great info here! I’m curious to know how you solved this problem?
I’m also facing this issue.

I interpreted the original post as asking how to display the JSON response as output from running newman on the command-line. Try adding this “test” to your collection:

pm.test("JSON echo", function () {
var jsonData = pm.response.json();
console.log(jsonData);
});

The output of “tests” shows when you run newman --so try adding a “test” to your collection, but instead of assert, do this:

console.log(pm.response.json());

Totally missed this…

Are you still having issues here?

You’re only getting the request body and response body here, the Headers wouldn’t be saved anywhere because there’s no reference to them.

They are going to different files because the file names are different.

You would need to use a different fs module function for them to be written to the same file.

That one would overwrite the other one…I think. Not 100% sure.

Yes. I am still having issue with this. I want to write request and response for each Tests in one file.What function has to use for this?

I would first change the file name to be the same for each so it’s writing to the same place and see what that’s giving you.

I think you’re probably going to need to use this rather than writeStream - https://nodejs.org/api/fs.html#fs_fs_appendfile_path_data_options_callback

Please let me know how did you achieve this ? i am also in need to write the output(Custom response i.e only Token value) in csv file.

Hi @danny-dainton:

I have got a straightforward question to you.
I have a set of collections in postman. I am using the GET to acquire some of the results from Sonar Cloud. The results are generated in the json format. Is there a way where we can save that onto a csv format ?
Any inputs for this is much appreciated and I can learn.

Regards,
Rahul

Welcome nrkrahul,

use custom reporting with new man you can save the result in csv format

Yes,
add this on block…

             // Salva a resposta no arquivo JSON
.on('request', (err, args) => {
            if (err) {
                console.error(`Somo text`, err);
            } else {
                // Captura o corpo da resposta
                const responseBody = args.response.stream.toString();

                if (responseBody) {
                    fs.writeFileSync(path.join(idResultsDir, `${someName}.json`), responseBody);
                    console.log(`saved);
                } else {
                    console.log(`Error.`);
                }
            }
        });