在 Postman 中将多级响应对象可视化为 Table

Visualize multi-level response object as Table in Postman

我想将 Postman 测试中的以下数据可视化为 Table,列中有 productpricequantity,列中有 Items行。可能有多个 shippingGroups。

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

                    ],
                    ...
            ]
        }
    ],

我在使用 {{#each response???}} 引用多级对象中的项目时遇到问题。预期格式应类似于:

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

有关 Postman 的更多信息 Table 可视化响应 here

鉴于您的响应示例,您可以使用如下内容:

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

这只是一个粗略的示例,需要重构,但它表明您可以在 table 中显示响应数据。