Un-nest (parse?) JSON Response

0

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?

[
    {
        "field1": null,
        "field2": null,
        "field3": null,
        "field4": null,
        "custom_fields": {
            "product_support": null,
            "consumer": null,
            "account_changes": null,
            "billing_support": null,
            "account": null,
            "collections": null,
            "resolution": null,
            "contact": null,
            "issue": null,
            "notes": null,
            "marketing_ad": null,
            "transport": null,
            "resolution": null,
            "modemgateway": null,
            "root_cause": null,
            "stb": null,
            "sale_made": null
        },
        "field5": null,
        "field6": null,
        "field7": null,
        "field8": null,
        "field9": null,
        "field10": null    

Hi @accortdr, welcome to the community! :wave:

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).

Hi Sivcan,

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?

Current Response:

   [
    {
        "field1": null,
        "field2": null,
        "field3": null,
        "field4": null,
        "custom_fields": {
            "product_support": null,
            "consumer": null,
            "account_changes": null,
            "billing_support": null,
            "account": null,
            "collections": null,
            "resolution": null,
            "contact": null,
            "issue": null,
            "notes": null,
            "marketing_ad": null,
            "transport": null,
            "resolution": null,
            "modemgateway": null,
            "root_cause": null,
            "stb": null,
            "sale_made": null
        },
        "field5": null,
        "field6": null,
        "field7": null,
        "field8": null,
        "field9": null,
        "field10": null
        }

]

Need to Response like this:

   [
    {
        "field1": null,
        "field2": null,
        "field3": null,
        "field4": null,
        "product_support": null,
        "consumer": null,
        "account_changes": null,
        "billing_support": null,
        "account": null,
        "collections": null,
        "resolution": null,
        "contact": null,
        "issue": null,
        "notes": null,
        "marketing_ad": null,
        "transport": null,
        "resolution": null,
        "modemgateway": null,
        "root_cause": null,
        "stb": null,
        "sale_made": null,
        "field5": null,
        "field6": null,
        "field7": null,
        "field8": null,
        "field9": null,
        "field10": null
        }

]

Oh, now I got you.

You could do something like this:

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

1 Like

I think this may have worked. What printed in the console is 29 collapsed arrays (I think) and the payload is formatted as I would like.

My question from here would be, are these results then saved as the response to this call? Or is it just printed in the console as a test?

The end state of this collection would be to build a custom connector that would retrieves the payload response in this format.

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.