簡體   English   中英

Javascript - 如何展平嵌套的 JSON object

[英]Javascript - How to Flatten a nested JSON object

我有一個嵌套的 JSON 輸入,如下所示,我想將其展平為預期的 output。 密鑰並不總是相同的,例如:u_summary、created_date、u_product,即可能有更多的密鑰。

有什么幫助嗎?

JSON 嵌套輸入:

var jsonUnflattenInput = 
    [
        
        {
            "u_summary": {
                "display_value": "Please check the data and set the company accordingly if everything is fine.",
                "value": "Please check the data."
            },
            "created_date": {
                "display_value": "2022-06-18 11:45:00",
                "value": "2022-05-29 21:42:01"
            },
            "u_product": {
                "display_value": "Undefined",
                "value": "2345567"
            }
        },
        {
            "u_summary": {
                "display_value": "╓ tool v14.3",
                "value": "14.3"
                },
            "created_date": {
                "display_value": "2022-03-18 11:45:00",
                "value": "2022-02-29 21:42:01"
            },
            "u_product": {
                "display_value": "test_product",
                "value": "1367"
            }
        }
    ]

預期 JSON Output:

[
    {
        "dv_u_summary": "Please check the data and set the company accordingly if everything is fine.",
        "u_summary": "Please check the data.",
        "dv_created_date": "2022-06-18 11:45:00",
        "created_date": "2022-05-29 21:42:01",
        "dv_u_product": "Undefined",
        "u_product": "2345567"
    },
    {
        "dv_u_summary": "╓ tool v14.3",
        "u_summary": "14.3",
        "dv_created_date": "2022-03-18 11:45:00",
        "created_date": "2022-02-29 21:42:01",
        "dv_u_product": "test_product",
        "u_product": "1367"
    }
]

在參考了一些博客后,我嘗試了以下代碼,但無法得到預期的 output。

function flatten(json, flattened, str_key) {
                for (var key in json) {
                  if (json.hasOwnProperty(key)) {
                    if (json[key] instanceof Object && json[key] != "") {
                      flatten(json[key], flattened, str_key + "." + key);
                    } else {
                      flattened[str_key + "." + key] = json[key];
                    }
                  }
                }
            }

        var flattened = {};
         
        flatten(jsonUnflattenInput, flattened, "");
        

        for (var key in flattened){
          console.log(key + ": " + flattened[key]);
        }

 function flatten(json) { var newJSON = []; for (var i = 0; i < json.length; i++) { var obj = {}; for (var key in json[i]) { if (json[i].hasOwnProperty(key)) { obj['dv_' + key] = json[i][key]['display_value']; obj[key] = json[i][key]['value']; } } newJSON.push(obj); } return newJSON; } var jsonUnflattenInput = [ { "u_summary": { "display_value": "Please check the data and set the company accordingly if everything is fine.", "value": "Please check the data." }, "created_date": { "display_value": "2022-06-18 11:45:00", "value": "2022-05-29 21:42:01" }, "u_product": { "display_value": "Undefined", "value": "2345567" } }, { "u_summary": { "display_value": "╓ tool v14.3", "value": "14.3" }, "created_date": { "display_value": "2022-03-18 11:45:00", "value": "2022-02-29 21:42:01" }, "u_product": { "display_value": "test_product", "value": "1367" } } ] console.log(flatten(jsonUnflattenInput))

這應該適用於提供的示例數據。

這可以按如下方式完成:

jsonUnflattenInput.map(o =>
  Object.keys(o).reduce((acc, k) => {
    acc['dv_' + k] = o[k].display_value;
    acc[k] = o[k].value;
    return acc;
  }, {})
);

請看下面的可運行代碼,看看它是如何工作的。

 var jsonUnflattenInput = [{ "u_summary": { "display_value": "Please check the data and set the company accordingly if everything is fine.", "value": "Please check the data." }, "created_date": { "display_value": "2022-06-18 11:45:00", "value": "2022-05-29 21:42:01" }, "u_product": { "display_value": "Undefined", "value": "2345567" } }, { "u_summary": { "display_value": "╓ tool v14.3", "value": "14.3" }, "created_date": { "display_value": "2022-03-18 11:45:00", "value": "2022-02-29 21:42:01" }, "u_product": { "display_value": "test_product", "value": "1367" } } ]; const result = jsonUnflattenInput.map(o => Object.keys(o).reduce((acc, k) => { acc['dv_' + k] = o[k].display_value; acc[k] = o[k].value; return acc; }, {}) ); console.log(result);

您可以為想要的鍵和函數的值和 map 新對象的條目獲取數組。

 const structure = [ ['dv_u_summary', o => o.u_summary.display_value], ['u_summary', o => o.u_summary.value], ['dv_created_date', o => o.created_date.display_value], ['created_date', o => o.created_date.value], ['dv_u_product', o => o.u_product.display_value], ['u_product', o => o.u_product.value] ], data = [{ u_summary: { display_value: "Please check the data and set the company accordingly if everything is fine.", value: "Please check the data." }, created_date: { display_value: "2022-06-18 11:45:00", value: "2022-05-29 21:42:01" }, u_product: { display_value: "Undefined", value: "2345567" } }, { u_summary: { display_value: "╓ tool v14.3", value: "14.3" }, created_date: { display_value: "2022-03-18 11:45:00", value: "2022-02-29 21:42:01" }, u_product: { display_value: "test_product", value: "1367" } }], result = data.map(o => Object.fromEntries(structure.map(([k, fn]) => [k, fn(o)]))); console.log(result);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

暫無
暫無

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

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