简体   繁体   English

JS - 将嵌套的 JSON 数组转换为单个对象

[英]JS - Transform Nested array of JSON into single Object

I want to transform the nested JSON structure into a single object, with the dynamic key I tried with below code but it's work only with one level, I need to write some recursive function, that I am struggling to write the code for n level of nested JSON.我想将嵌套的 JSON 结构转换为单个对象,使用我在下面的代码中尝试过的动态键,但它仅适用于一个级别,我需要编写一些递归函数,我正在努力编写 n 级的代码嵌套的 JSON。 Please advise.请指教。

   data.map((e) => {
   for (let key in e) {
     if (typeof e[key] === "object") {
       for (let onLevel in e[key]) {
         e[key + "." + onLevel] = e[key][onLevel];
       }
     }
   }
 });

Example例子

Input JSON输入 JSON

[{
  "Id": "0hb3L00000000jkQAA",
  "Name": "P-2797",
  "ContactEncounterId": "0ha3L000000001qQAA",
  "StartTime": "2020-06-27T11:00:00.000Z",
  "EncounterDuration": 25,
  "ContactEncounter": {
    "Name": "Grocery Shopping 17",
    "LocationId": "1313L0000004ENlQAM",
    "Id": "0ha3L000000001qQAA",
    "Location": {
      "Name": "Waitrose",
      "LocationType": "Site",
      "Id": "1313L0000004ENlQAM"
    }
  }
}]

OutPut JSON输出 JSON

[{
  "Id": "0hb3L00000000jkQAA",
  "Name": "P-2797",
  "ContactEncounterId": "0ha3L000000001qQAA",
  "StartTime": "2020-06-27T11:00:00.000Z",
  "EncounterDuration": 25,
  "ContactEncounter.Name": "Grocery Shopping 17",
  "ContactEncounter.LocationId": "1313L0000004ENlQAM",
  "ContactEncounter.Id": "0ha3L000000001qQAA",
  "ContactEncounter.Location.Name": "Waitrose",
  "ContactEncounter.Location.LocationType": "Site",
  "ContactEncounter.Location.Id": "1313L0000004ENlQAM"
}]

As you said, you need to create a recursion to get deeper into the object.正如你所说,你需要创建一个递归来更深入地了解对象。 Which means, you would have to track the path where you are.这意味着,您必须跟踪您所在的路径。

You could solve it in the following way你可以通过以下方式解决

 const input = [{ "Id": "0hb3L00000000jkQAA", "Name": "P-2797", "ContactEncounterId": "0ha3L000000001qQAA", "StartTime": "2020-06-27T11:00:00.000Z", "EncounterDuration": 25, "ContactEncounter": { "Name": "Grocery Shopping 17", "LocationId": "1313L0000004ENlQAM", "Id": "0ha3L000000001qQAA", "Location": { "Name": "Waitrose", "LocationType": "Site", "Id": "1313L0000004ENlQAM" } } } ]; function merge( source, target = {}, ...parents) { for (let [key, value] of Object.entries( source ) ) { const path = (parents || []).concat( key ); if (typeof value === 'object') { merge( value, target, ...path ); continue; } target[path.join('.')] = value; } return target; } console.log( merge( input[0] ) );

Or in the following way, where you just use Object.assign to assign the results of the deeper search into your current object.或者通过以下方式,您只需使用Object.assign将更深层次搜索的结果分配到当前对象中。

 const input = [{ "Id": "0hb3L00000000jkQAA", "Name": "P-2797", "ContactEncounterId": "0ha3L000000001qQAA", "StartTime": "2020-06-27T11:00:00.000Z", "EncounterDuration": 25, "ContactEncounter": { "Name": "Grocery Shopping 17", "LocationId": "1313L0000004ENlQAM", "Id": "0ha3L000000001qQAA", "Location": { "Name": "Waitrose", "LocationType": "Site", "Id": "1313L0000004ENlQAM" } } } ]; function merge( source, ...parents) { const mergedValue = {}; for (let [key, value] of Object.entries( source ) ) { const path = (parents || []).concat( key ); if (typeof value === 'object') { Object.assign( mergedValue, merge( value, ...path ) ); continue; } mergedValue[path.join('.')] = value; } return mergedValue; } console.log( merge( input[0] ) );

Here is another approach by using the second parameter and passing the key when finding a nth level object.这是另一种使用第二个参数并在查找第 n 级对象时传递键的方法。

 const obj = { "Id": "0hb3L00000000jkQAA", "Name": "P-2797", "ContactEncounterId": "0ha3L000000001qQAA", "StartTime": "2020-06-27T11:00:00.000Z", "EncounterDuration": 25, "ContactEncounter": { "Name": "Grocery Shopping 17", "LocationId": "1313L0000004ENlQAM", "Id": "0ha3L000000001qQAA", "Location": { "Name": "Waitrose", "LocationType": "Site", "Id": "1313L0000004ENlQAM" } } } function flattenObj(obj, param) { let newObj = {}; for (let key in obj) { if (typeof obj[key] === 'object') { newObj = { ...newObj, ...flattenObj(obj[key], key + '.') } } else { newObj[param + key] = obj[key] } } return newObj; } console.log(flattenObj(obj, ''))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM