1

I would like to visualize the data below in Postman Tests as a Table with product, price and quantity in columns and Items in rows. There might be multiple shippingGroups.

{
   ...
   "companyGroups": [
        {
            ...
            "shippingGroups": [
                {
                    "id": 1,
                    "items": [
                        {
                            "product": "Product A",
                            "price": 2,
                            "quantity": 1,
                        },
                          {
                            "product": "Product B",
                            "price": 4,
                            "quantity": 4,
                        }

                    ],
                    ...
            ]
        }
    ],

I have trouble using the {{#each response???}} referring to items within multiple level objects. Expected format should be something like :

   <table>
        <tr>
            <th>Product</th>
            <th>Price</th>
            <th>Quantity</th>
        </tr>

        {{#each response???}}
            <tr>
                <td>{{???product}}</td>
                <td>{{???price}}</td>
                <td>{{???quantity}}
            </tr>
        {{/each}}
    </table>

More info on Postman Table Visualizing Response here

Quang T
  • 15
  • 6

1 Answers1

1

Given your response example, you could use something like this:

const template = `
   <table>
        <tr>
            <th>Product</th>
            <th>Price</th>
            <th>Quantity</th>
        </tr>

        {{#each responseData}}
        {{#each items}}
            <tr>
                <td>{{product}}</td>
                <td>{{price}}</td>
                <td>{{quantity}}
            </tr>
        {{/each}}
        {{/each}}
    </table>
`;

let responseData = []

_.each(pm.response.json().companyGroups, (item) => {
    _.each(item.shippingGroups, (nestedItem) => {
        responseData.push(nestedItem)
    })
})

pm.visualizer.set(template, { responseData })

This is just a rough example and would need to be refactored but it shows that you can show the response data in the table.

Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
  • Hi, wanted to get your attention for a Postman question I just posted, and sorry if I am imposing. Regards. https://stackoverflow.com/questions/63263561/postman-visualization-how-to-render-image-url-in-simple-html-img-tag – Sabuncu Aug 05 '20 at 10:49
  • I tried to refactor using your approach, but I was not successful. I specifically chose my example because it returns a very simple construct that does not repeat and therefore does not require looping. Your solution is more geared for a repeating data set. – Sabuncu Aug 05 '20 at 12:00
  • Are you talking about this? https://stackoverflow.com/a/63264138/6028443, if so let's talk about that in that context :D – Danny Dainton Aug 05 '20 at 12:03
  • 1
    Danny, I APOLOGIZE in capital letters. I was totally confused as I have been working a lot. Thank you, I accepted your answer for my question. :) – Sabuncu Aug 05 '20 at 13:42