I have the following JSON body and want to un-nest the custom fields array. The end goal of this is to request this data and export to csv and I want each field to have its own column, including the custom fields which right now are all shoved into a single column for each entry. Any advice on how to do this in Postman?
The response that you shared seems to be broken towards the end.
Iβll assume that your array has only 1 item.
To access the custom_fields array, you can write the following in the test script:
let customFields = _.get(pm.response.json(), '0.custom_fields');
console.log({ customFields });
Also, you can go through this thread to convert JSON to a CSV, and in case you want to write the CSV data to a file then you can use this template which will help you write your responses from Postman to a File.
Make sure you change the fileExtension to csv (Refer the docs of the template for more information).
Thank you for your solution. I am not sure if I am following the logic though. I inserted your test script and am still getting the same response. See below for what I am currently receiving vs what I would expect to see given your solution. Please note how the custom fields encapsulated the several indented fields in the initial response where as I need to unnest them in the second response so that they are each their own field. Does this make sense?
let resp = pm.response.json();
Object.assign(resp[0], resp[0].custom_fields);
// Remove the existing custom_fields property, since we already have put the properties of custom_fields into the upper level
delete resp[0].custom_fields;
console.log({ resp }); // Check Postman Console to see the value - matches what you wanted it as
This is a javascript variable resp which will only be available in your test script.
You can perform your tests etc. on it, or you can store it as a Postman variable to use it across multiple requests.
It canβt be saved as the response of the request.