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


一、JSON结构转换是什么?

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

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

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

JSON结构转换通常在数据处理、数据清洗、数据分析等场景中广泛应用。通过结构转换,可以根据需求定制化地处理JSON数据,使其符合特定的业务逻辑或数据格式要求。
为此我们提供了三个简单开源的类库(JavaScript类库Java类库.Net类库),接下来我们对JS类库进行详细讲解。

二、案例之《JSON对象 To JSON对象》

源JSON结构:

{
    "a": {
        "k": "v",
        "a": "b"
    }
}

目标JSON结构:

{
    "b1": {
        "k1": "v1"
    }
}

转换需求:
以下需求分别执行

  1. 将源结构的“a”键替换到目标结构的“b1”键
  2. 将源结构的“a”键追加到目标结构的“b1”值
  3. 将源结构的“a”键替换到目标结构的“b1”值
  4. 将源结构的“a”值替换到目标结构的“b1”键
  5. 将源结构的“a”值追加到目标结构的“b1”中
  6. 将源结构的“a”值替换到目标结构的“b1”中

三、代码实现

1. 将源结构的“a”键替换到目标结构的“b1”键


import JsonTranferUtil from './json_transfer_new'
const jsonOrg = { a: { k: "v", a: "b" } };

const jsonAim = { b1: { k1: "v1" } };
const mappings = [
  {
    "AimJsonPath": "root.b1",
    "OrgJsonPath": "root.a",
    "TranType": 1
  }
];

/// 转换类型
/// 1:源Key->目标Key
/// 2:源Key->目标Value
/// 3:源Value->目标Key
/// 4:源Value->目标Value
const mappings = [
  {
    "AimJsonPath": "root.b1",
    "OrgJsonPath": "root.a",
    "TranType": 1
  }
];

/*******************对象转对象***************** */
let jsonTranferUtil = new JsonTranferUtil(jsonOrg, jsonAim, mappings);
let result = jsonTranferUtil.tranJson();
console.log("********最终转换结果***************")
console.log(result, 88888888888)

执行结果如下:
在这里插入图片描述

2. 将源结构的“a”键追加到目标结构的“b1”值

const mappings = [
  {
    "AimJsonPath": "root.b1",
    "OrgJsonPath": "root.a",
    "TranType": 2
  }
];

/*******************对象转对象***************** */
let jsonTranferUtil = new JsonTranferUtil(jsonOrg, jsonAim, mappings);
let result = jsonTranferUtil.tranJson();
console.log("********最终转换结果***************")
console.log(result, 88888888888)

在这里插入图片描述

3. 将源结构的“a”键替换到目标结构的“b1”值

const mappings = [
  {
    "AimJsonPath": "root.b1",
    "OrgJsonPath": "root.a",
    "TranType": 2,
    "Options": {
      "KeyInitIndex": 0,
      "AddElementsOption": "2", 
      "TranOP": "1", 
      "TranWay": "1" 
    }
  }
];

/*******************对象转对象***************** */
let jsonTranferUtil = new JsonTranferUtil(jsonOrg, jsonAim, mappings);
let result = jsonTranferUtil.tranJson();
console.log("********最终转换结果***************")
console.log(result, 88888888888)

在这里插入图片描述
4. 将源结构的“a”值替换到目标结构的“b1”键

const mappings = [
  {
    "AimJsonPath": "root.b1",
    "OrgJsonPath": "root.a",
    "TranType": 3,
    "Options": {
      "KeyInitIndex": 0,
      "AddElementsOption": "1", 
      "TranOP": "1", 
      "TranWay": "1" 
    }
  }
];

/*******************对象转对象***************** */
let jsonTranferUtil = new JsonTranferUtil(jsonOrg, jsonAim, mappings);
let result = jsonTranferUtil.tranJson();
console.log("********最终转换结果***************")
console.log(result, 88888888888)

在这里插入图片描述
5. 将源结构的“a”值追加到目标结构的“b1”中

const mappings = [
  {
    "AimJsonPath": "root.b1",
    "OrgJsonPath": "root.a",
    "TranType": 4,
    "Options": {
      "KeyInitIndex": 0,
      "AddElementsOption": "1", 
      "TranOP": "1", 
      "TranWay": "1" 
    }
  }
];

/*******************对象转对象***************** */
let jsonTranferUtil = new JsonTranferUtil(jsonOrg, jsonAim, mappings);
let result = jsonTranferUtil.tranJson();
console.log("********最终转换结果***************")
console.log(result, 88888888888)

在这里插入图片描述
6. 将源结构的“a”值替换到目标结构的“b1”中

const mappings = [
  {
    "AimJsonPath": "root.b1",
    "OrgJsonPath": "root.a",
    "TranType": 4,
    "Options": {
      "KeyInitIndex": 0,
      "AddElementsOption": "2", 
      "TranOP": "1", 
      "TranWay": "1" 
    }
  }
];

/*******************对象转对象***************** */
let jsonTranferUtil = new JsonTranferUtil(jsonOrg, jsonAim, mappings);
let result = jsonTranferUtil.tranJson();
console.log("********最终转换结果***************")
console.log(result, 88888888888)

在这里插入图片描述

四、在线转换工具

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

工具地址:数据转换工具
在这里插入图片描述

五、技术资料

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

御控物联_姜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值