【御控物联】JavaScript JSON结构转换(19):数组To对象——规则属性重组

文章目录


一、JSON结构转换是什么?

JSON结构转换指的是将一个JSON对象或JSON数组按照一定规则进行重组、筛选、映射或转换,生成新的JSON对象或数组的过程。这种转换可以包括改变JSON数据的结构、提取特定字段、合并多个JSON数据,或者对数据进行计算和处理等操作。

在JSON结构转换中,常见的操作包括:

  • 提取字段:从一个JSON对象中提取特定字段,生成新的JSON对象。
  • 过滤数据:根据条件过滤JSON数据,生成符合条件的新JSON对象或数组。
  • 映射转换:将一个JSON对象中的字段映射到另一个字段,生成新的JSON对象。
  • 合并数据:将多个JSON对象或数组合并成一个新的JSON对象或数组。

JSON结构转换通常在数据处理、数据清洗、数据分析等场景中广泛应用。通过结构转换,可以根据需求定制化地处理JSON数据,使其符合特定的业务逻辑或数据格式要求。

为此我们提供了一个简单开源的JS类库,接下来我们对此类库进行详细讲解。

二、术语解释

1.规则属性

规则属性常用于批量属性匹配转换
规则属性包含两种

  • 对象的" * ",如a.*、a.*.name、a.*.*、a.*.*.age
  • 数组的" [*] ",a[*]、a[*].name

三、案例之《JSON数组 To JSON对象》

源JSON结构:

json 复制代码
{
  "b1": [
    [
      {
        "k1_child": "v1_child"
      }
    ],
    [
      {
        "k2_child": "v2_child"
      }
    ],
    [
      {
        "k3_child": "v3_child"
      }
    ]
  ]
}

目标JSON结构:

json 复制代码
{
  "a": {
    "k": "v"
  }
}

转换需求:

以下需求分别执行

  1. 将源结构的"b1[*]"值追加到目标结构的"a"值
  2. 将源结构的"b1[*]"值替换到目标结构的"a"值
  3. 将源结构的"b1[*]"值追加到目标结构的"a"值(TranOP=2:将源子元素复制到目标,为源子元素新生成Key)
  4. 将源结构的"b1[*]"键追加到目标结构的"a"值(TranOP=2:将源子元素复制到目标,为源子元素新生成Key)
  5. 将源结构的"b1[*][*]"值追加到目标结构的"a"值

四、代码实现

1.将源结构的"b1[*]"值追加到目标结构的"a"值

js 复制代码
import JsonTranferUtil from './json_transfer'

/************************数组转对象   示例数据 ********************** */

/// 转换类型
/// 1:源Key->目标Key
/// 2:源Key->目标Value
/// 3:源Value->目标Key
/// 4:源Value->目标Value
const mappings = [
  {
    "OrgJsonPath": "root.b1[*]",
    "AimJsonPath": "root.a",
    "TranType": 4,
    "Options": {
      "KeyInitIndex": 0,
      "AddElementsOption": "1",
      "TranOP": "1",
      "TranWay": "1"
    }
  }
];
const jsonOrg = {
    "b1": [
      [
        {
          "k1_child": "v1_child"
        }
      ],
      [
        {
          "k2_child": "v2_child"
        }
      ],
      [
        {
          "k3_child": "v3_child"
        }
      ]
    ]
  };
const jsonAim ={
    "a": {
      "k": "v"
    }
  };

/*******************数组转对象    测试程序***************** */

let jsonTranferUtil = new JsonTranferUtil(jsonOrg, jsonAim, mappings);
let result = jsonTranferUtil.tranJson();
console.log("*************************最终转换结果*********************************")
console.log(JSON.stringify(result), 9999999999999)

执行结果如下:

2.将源结构的"b1[*]"值替换到目标结构的"a"值

js 复制代码
import JsonTranferUtil from './json_transfer'

/************************数组转对象   示例数据 ********************** */

/// 转换类型
/// 1:源Key->目标Key
/// 2:源Key->目标Value
/// 3:源Value->目标Key
/// 4:源Value->目标Value
const mappings = [
  {
    "OrgJsonPath": "root.b1[*]",
    "AimJsonPath": "root.a",
    "TranType": 4,
    "Options": {
      "KeyInitIndex": 0,
      "AddElementsOption": "2",
      "TranOP": "1",
      "TranWay": "1"
    }
  }
];
const jsonOrg = {
    "b1": [
      [
        {
          "k1_child": "v1_child"
        }
      ],
      [
        {
          "k2_child": "v2_child"
        }
      ],
      [
        {
          "k3_child": "v3_child"
        }
      ]
    ]
  };
const jsonAim ={
    "a": {
      "k": "v"
    }
  };

/*******************数组转对象    测试程序***************** */

let jsonTranferUtil = new JsonTranferUtil(jsonOrg, jsonAim, mappings);
let result = jsonTranferUtil.tranJson();
console.log("*************************最终转换结果*********************************")
console.log(JSON.stringify(result), 9999999999999)

执行结果如下:

3.将源结构的"b1[*]"值追加到目标结构的"a"值(TranOP=2:将源子元素复制到目标,为源子元素新生成Key)

js 复制代码
import JsonTranferUtil from './json_transfer'

/************************数组转对象   示例数据 ********************** */


/// 转换类型
/// 1:源Key->目标Key
/// 2:源Key->目标Value
/// 3:源Value->目标Key
/// 4:源Value->目标Value
const mappings = [
  {
    "OrgJsonPath": "root.b1[*]",
    "AimJsonPath": "root.a",
    "TranType": 4,
    "Options": {
      "KeyInitIndex": 0,
      "AddElementsOption": "1",
      "TranOP": "2",
      "TranWay": "1"
    }
  }
];
const jsonOrg = {
    "b1": [
      [
        {
          "k1_child": "v1_child"
        }
      ],
      [
        {
          "k2_child": "v2_child"
        }
      ],
      [
        {
          "k3_child": "v3_child"
        }
      ]
    ]
  };
const jsonAim ={
    "a": {
      "k": "v"
    }
  };

/*******************数组转对象    测试程序***************** */

let jsonTranferUtil = new JsonTranferUtil(jsonOrg, jsonAim, mappings);
let result = jsonTranferUtil.tranJson();
console.log("*************************最终转换结果*********************************")
console.log(JSON.stringify(result), 9999999999999)

执行结果如下:

4.将源结构的"b1[*]"键追加到目标结构的"a"值(TranOP=2:将源子元素复制到目标,为源子元素新生成Key)

js 复制代码
import JsonTranferUtil from './json_transfer'

/************************数组转对象   示例数据 ********************** */

/// 转换类型
/// 1:源Key->目标Key
/// 2:源Key->目标Value
/// 3:源Value->目标Key
/// 4:源Value->目标Value
const mappings = [
  {
    "OrgJsonPath": "root.b1[*]",
    "AimJsonPath": "root.a",
    "TranType": 2,
    "Options": {
      "KeyInitIndex": 0,
      "AddElementsOption": "1",
      "TranOP": "2",
      "TranWay": "1"
    }
  }
];
const jsonOrg = {
    "b1": [
      [
        {
          "k1_child": "v1_child"
        }
      ],
      [
        {
          "k2_child": "v2_child"
        }
      ],
      [
        {
          "k3_child": "v3_child"
        }
      ]
    ]
  };
const jsonAim ={
    "a": {
      "k": "v"
    }
  };

/*******************数组转对象    测试程序***************** */

let jsonTranferUtil = new JsonTranferUtil(jsonOrg, jsonAim, mappings);
let result = jsonTranferUtil.tranJson();
console.log("*************************最终转换结果*********************************")
console.log(JSON.stringify(result), 9999999999999)

执行结果如下:

5.将源结构的"b1[*][*]"值追加到目标结构的"a"值
注意:这里TranOP=2,如果使用TranOP=1 a的新增值会因为同一个Key("0")而覆盖

js 复制代码
import JsonTranferUtil from './json_transfer'

/************************数组转对象   示例数据 ********************** */

/// 转换类型
/// 1:源Key->目标Key
/// 2:源Key->目标Value
/// 3:源Value->目标Key
/// 4:源Value->目标Value
const mappings = [
  {
    "OrgJsonPath": "root.b1[*][*]",
    "AimJsonPath": "root.a",
    "TranType": 4,
    "Options": {
      "KeyInitIndex": 0,
      "AddElementsOption": "1",
      "TranOP": "2",
      "TranWay": "1"
    }
  }
];
const jsonOrg = {
    "b1": [
      [
        {
          "k1_child": "v1_child"
        }
      ],
      [
        {
          "k2_child": "v2_child"
        }
      ],
      [
        {
          "k3_child": "v3_child"
        }
      ]
    ]
  };
const jsonAim ={
    "a": {
      "k": "v"
    }
  };

/*******************数组转对象    测试程序***************** */

let jsonTranferUtil = new JsonTranferUtil(jsonOrg, jsonAim, mappings);
let result = jsonTranferUtil.tranJson();
console.log("*************************最终转换结果*********************************")
console.log(JSON.stringify(result), 9999999999999)

执行结果如下:

五、在线转换工具

为了让使用者更加方便的配置出映射关系,为此开发了一套在线转换工具,可在工具中通过拖拽即可配置想要的结构转换关系,并可对转换关系所能实现的效果实时进行预览更改。

工具地址:数据转换工具

六、技术资料

相关推荐
通信仿真实验室21 分钟前
(10)MATLAB莱斯(Rician)衰落信道仿真1
开发语言·matlab
勿语&24 分钟前
Element-UI Plus 暗黑主题切换及自定义主题色
开发语言·javascript·ui
吾爱星辰4 小时前
Kotlin 处理字符串和正则表达式(二十一)
java·开发语言·jvm·正则表达式·kotlin
ChinaDragonDreamer4 小时前
Kotlin:2.0.20 的新特性
android·开发语言·kotlin
IT良4 小时前
c#增删改查 (数据操作的基础)
开发语言·c#
Kalika0-05 小时前
猴子吃桃-C语言
c语言·开发语言·数据结构·算法
_.Switch5 小时前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
一路向前的月光5 小时前
Vue2中的监听和计算属性的区别
前端·javascript·vue.js
长路 ㅤ   5 小时前
vue-live2d看板娘集成方案设计使用教程
前端·javascript·vue.js·live2d
代码雕刻家5 小时前
课设实验-数据结构-单链表-文教文化用品品牌
c语言·开发语言·数据结构