【问题标题】:Javascript json into nested jsonJavascript json 转换成嵌套 json
【发布时间】:2020-03-26 16:43:45
【问题描述】:

我有一个巨大的 json,我从 Excel 表中获取。 我得到的数据是对象数组,一个对象如下所示。

[
  {
    "key": "guid",
    "parent": "id__guid"
  },
  {
    "key": "version",
    "parent": "id__version"
  },
  {
    "key": "register",
    "parent": "register"
  },
  {
    "key": "offloadId",
    "parent": "offloadId"
  },
  {
    "key": "action",
    "parent": "action"
  },
  {
    "key": "reported",
    "parent": "reported"
  },
  {
    "key": "control",
    "parent": "control"
  },
  {
    "key": "AppNum",
    "parent": "Identification__AppNum"
  },
  {
    "key": "DataTp",
    "parent": "Identification__DataTp"
  },
  {
    "key": "DtOgWatchDt",
    "parent": "Identification__DtOgWatchDt"
  },
  {
    "key": "DtPendingWatchDt",
    "parent": "Identification__DtPendingWatchDt"
  },
  {
    "key": "IssRef",
    "parent": "Identification__IssRef"
  },
  {
    "key": "ImgRef",
    "parent": "Identification__ImgRef"
  },
  {
    "key": "Register",
    "parent": "Identification__Register"
  },
  {
    "key": "-",
    "parent": "Identification__ImgRefFullPub"
  },
  {
    "key": "DtAppDt",
    "parent": "Dates__DtAppDt"
  },
  {
    "key": "IdxNam",
    "parent": "Description__IdxNam"
  },
  {
    "key": "Clms",
    "parent": "Description__Clms"
  },
  {
    "key": "Disclaims",
    "parent": "Description__Disclaims"
  },
  {
    "key": "LglStsCd",
    "parent": "Status__LglStsCd"
  },
  {
    "key": "UsPtoStsCd",
    "parent": "Status__UsPtoStsCd"
  },
  {
    "key": "PtoStsCdDt",
    "parent": "Status__PtoStsCdDt"
  },
  {
    "key": "StsFlag",
    "parent": "Status__StsFlag"
  },
  {
    "key": "SrcInd",
    "parent": "Status__SrcInd"
  },
  {
    "key": "LglStsCdNorm",
    "parent": "Status__LglStsCdNorm"
  }
]

我想把它转换成这种嵌套的json格式。

[
  {
    name: "Identification",
    fields: [
      {
        "key": "AppNum",
        "parent": "Identification__AppNum"
      },
      {
        "key": "DataTp",
        "parent": "Identification__DataTp"
      },
      {
        "key": "DtOgWatchDt"
      },
      {
        "key": "DtPendingWatchDt"
      },
      {
        "key": "IssRef"
      },
      {
        "key": "ImgRef"
      },
      {
        "key": "Register"
      },
      {
        "key": "ImgRefFullPub"
      },
      {
        "key": "guid"
      },
      {
        "key": "version"
      },
      {
        "key": "offloadid"
      },
      {
        "key": "reported"
      },
      {
        "key": "control"
      }
    ]
  },
  {
    name: "Description",
    fields: [
      {
        "key": "IdxNam"
      },
      {
        "key": "Clms"
      },
      {
        "key": "Disclaims"
      }
    ]
  },
  {
    name: "Status",
    fields: [
      {
        "key": "UsPtoStsCd"
      },
      {
        "key": "PtoStsCdDt"
      },
      {
        "key": "LglStsCd"
      },
      {
        "key": "StsFlag"
      },
      {
        "key": "SrcInd"
      },
      {
        "key": "LglStsCdNorm"
      }
    ]
  },
  {
    name: "Dates",
    fields: [
      {
        "key": "DtAppDt"
      }
    ]
  }
]

如您所见,根据父键,我们必须创建嵌套结构。 我已经尝试了所有方法,我也在谷歌上搜索了很多,但运气不好。

任何帮助将不胜感激。

【问题讨论】:

    标签: javascript json object nested


    【解决方案1】:

    您需要创建一个新的对象数组,然后循环遍历原始数组并根据该数组向新数组中添加条目。例如——

    const input = [
      {
        "key": "guid",
        "parent": "id__guid"
      },
      {
        "key": "version",
        "parent": "id__version"
      }
      // et cetera... your input
    ];
    
    // these are buckets.
    const transformedObject = {
      "Identification": []
    }
    
    // First we put the data into the right buckets
    input.forEach((entry) => {
    
      // Create a new object with the key
      const newObject = { key: entry.key };
    
      // By default, the parent seems to be "Identification"
      let parentKey = "Identification";
    
      // Find out if the parent's name has an underscore?
      if (entry.parent.split("__").length > 1) {
    
        // If so, that's the new parent
        parentKey = entry.parent.split("__")[0];
    
      }
    
      // If there isn't an array for this parent, make one
      if (!transformedObject[parentKey]) {
        transformedObject[parentKey] = [];
      }
    
      transformedObject[parentKey].push(newObject);  
    
    })
    
    const output = [];
    
    // Then we need to shape the data
    Object.keys(transformedObject).forEach((parentKey) => {
      const parentGroup = {
        name: parentKey,
        fields: transformedObject[parentKey]
      };
      output.push(parentGroup);
    });
    
    
    console.log(output);

    您会注意到这并没有让您一路走好。 “id”前缀似乎已合并到“Identification”前缀中,并且您希望保留其中一些对象的父值。您需要一些条件或地图或其他东西才能使该部分正常工作。但我希望这是一个开始!

    【讨论】:

    • 您好,谢谢您的回复。我检查了这段代码。当我们有父属性 2 级别时,此代码有效,但如果超过此级别,则无法正常工作。例如,如果我们有这样的父母 OwnerAffiliates__Correspondents__Correspondent__Name 它只需要 OwnerAffiliatesName 并跳过 CorrespondentsCorrespondent
    猜你喜欢
    • 2016-08-09
    • 2021-07-15
    • 1970-01-01
    • 1970-01-01
    • 2010-11-15
    • 2020-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多