簡體   English   中英

如何從 Postman 測試腳本中的嵌套和變化的 JSON 有效載荷中提取對象/鍵名稱?

[英]How can I extract object/key names from nested and varying JSON payload in a Postman Test script?

我是一個老派的 C++ 程序員 - 試圖掌握 Postman、JSON、REST API 等,並努力......

我正在嘗試編寫一個 Postman 測試來可視化一些 JSON 響應數據——我想以表格格式顯示這些數據,並將一些合並的鍵名稱作為列標題。

問題是響應中的數據項數量可能會有所不同,鍵名也可能會有所不同——具體取決於輸入參數。

所以說,對於以下具有兩個數據項的 JSON:

{
    "data": [
        {
            "input": [
                {
                    "value": "ABC",
                    "identifierType": "a1"
                }
            ],
            "output": [
                {
                    "value": "BT",
                    "identifierType": "b1",
                    "name": "BT GROUP",
                    "status": "Active",
                    "classification": "Ordinary"
                }
            ]
        },
        {
            "input": [
                {
                    "value": "BCD",
                    "identifierType": "a1"
                }
            ],
            "output": [
                {
                    "value": "EFG",
                    "identifierType": "b1",
                    "name": "SIEMENS",
                    "status": "Active",
                    "classification": "Ordinary"
                }
            ]
        }
    ]
}

我想最終得到一個包含列標題的集合,看起來像這樣:[“輸入值”、“輸入標識符類型”、“輸出值”、“輸出標識符類型”、“輸出名稱”、“輸出狀態”、“輸出分類”]

我可以通過以下方式參與其中:

function parseData(response, host) {
    const results = response.map(elem => (
        [
            elem.input[0].value,
            elem.input[0].identifierType,
            elem.output[0].value,
            elem.output[0].identifierType,
            elem.output[0].name,
            elem.output[0].status,
            elem.output[0].classification,
        ] 
    ));
    const headers = [];
    for (const key in response[0].input[0]){
        headers.push(`Input ${key}`)
    }
    for (const key in response[0].output[0]){
        headers.push(`Output ${key}`)
    }

    return [results, headers]
}

這給了我所需的標題:[“輸入值”、“輸入標識符類型”、“輸出值”、“輸出標識符類型”、“輸出名稱”、“輸出狀態”、“輸出分類”]

但是,我想讓它更通用,即不必在 for 循環中指定input[0]output[0] - 因為這些鍵名可能因不同的查詢響應而不同。

我確實在 Postman 論壇上問過這個問題,有人提供了一個有用的代碼片段,允許我提取“輸入”和“輸出”名稱。

for (const _outer in response[0]) {
    for (const _inner in _outer) {
        for (const key in _inner) {
            headers.push(`${_outer} ${key}`)
        }
    }
}

但這只給我:標題的 ["input 0", "input 0", "input 0", "input 0", "input 0", "output 0' …]。出於某種原因,我無法訪問內部鍵,例如值、名稱、標識符類型、狀態等

有人可以建議上面哪里出了問題/如何得到我想要的東西嗎? 謝謝。

好的 - 我設法在 Postman 論壇上得到了答案 - 謝謝。

解決方案是:

for (const outerkey in response[0]){
    for (const innerkey in response[0][`${outerkey}`][0]){
        headers.push(`${outerkey} ${innerkey}`)
    }
}

並給了我: “輸入值”,“輸入標識符類型”,“輸出值”,“輸出標識符類型”,“輸出名稱”,...]

更新:數據項提取仍然使用鍵名進行硬編碼(例如elem.input[0].identifierType等),因此我也替換了它以提供以下解決方案:

function parseData(response, host) {
    const results = response.map(function(val, index){
        temp = [];
        for (const outerkey in val){
            for (const innerkey in val[`${outerkey}`][0]){
                temp.push(val[`${outerkey}`][0][`${innerkey}`]);
            }
        }
        return temp
    }
    );
    
    const headers = [];

    for (const outerkey in response[0]){
        for (const innerkey in response[0][`${outerkey}`][0]){
            headers.push(`${outerkey} ${innerkey}`)
        }
    }
    return [results, headers]
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM