当前位置: 首页 > 知识库问答 >
问题:

JSON 到嵌套的 JSON 嵌套数组使用 Jolt 转换

禹德水
2023-03-14

如何使用jolt转换将平面JSON转换为嵌套JSON?我对JSON和jolt是新手。

输入:

[
  {
    "Id": 1,
    "number": 6,
    "name": "axa",
    "code": "wewe",
    "amount": "100",
    "currency": "doller",
    "othercurrency": "aug",
    "subfund": "axa1",
    "noOfUnits": 0,
    "unitPrice": 0,
    "insurerId": ""
  },
  {
    "Id": 2,
    "number": 6,
    "name": "visa",
    "code": "wewe",
    "amount": "100",
    "currency": "doller",
    "othercurrency": "aug",
    "subfund": "visa1",
    "noOfUnits": 0,
    "unitPrice": 0,
    "insurerId": ""
  },
  {
    "Id": 1,
    "number": 6,
    "name": "master",
    "code": "qqq",
    "amount": "100",
    "currency": "doller",
    "othercurrency": "aug",
    "subfund": "master1",
    "noOfUnits": 0,
    "unitPrice": 0,
    "insurerId": ""
  }
]

预期输出:

[
  {
    "id": "1",
    "number": 6,
    "funds": [
      {
        "name": "axa",
        "code": "wewe",
        "balance": {
          "amount": 100,
          "currency": "doller",
          "othercurrency": "aug"
        },
        "subFunds": [
          {
            "name": "axa1",
            "noOfUnits": 0,
            "unitPrice": 0
          }
        ],
        "insurerId": ""
      },
      {
        "name": "master",
        "code": "qqq",
        "balance": {
          "amount": 100,
          "currency": "doller",
          "othercurrency": "aug"
        },
        "subFunds": [
          {
            "name": "master1",
            "noOfUnits": 0,
            "unitPrice": 0
          }
        ],
        "insurerId": ""
      }
    ]
  },
  {
    "id": "2",
    "number": 6,
    "funds": [
      {
        "name": "visa",
        "code": "wewe",
        "balance": {
          "amount": 100,
          "currency": "doller",
          "othercurrency": "aug"
        },
        "subFunds": [
          {
            "name": "visa1",
            "noOfUnits": 0,
            "unitPrice": 0
          }
        ],
        "insurerId": ""
      }
    ]
  }
]

编辑:我想在< code>SubFunds中添加一个新字段,但是新字段不在JSON文件中,它是计算字段,我可以按原样添加示例吗

"subFunds": [ { "name": "axa1", "noOfUnits": 0, "unitPrice": 0 } 
// to be 
"subFunds": [ { "name": "axa1", "noOfUnits": 0, "unitPrice": 0, "Total": unitPrice * noOfUnits, "SubTotal": Total+300 }

并重命名字段:

"Id": "@(1,Id).&"
// to 
"Id": "@(1,Id).k_id"
"amount": "@(1,Id).funds[&1].balance.&"
// to 
"amount": "@(1,Id).funds[&1].k_bal"
"noOfUnits": "@(1,Id).funds[&1].subFunds[&1].&"
// to 
"noOfUnits": "@(1,Id).funds[&1].subFunds[&1].k_units"

共有1个答案

解明辉
2023-03-14

事实上,您可以将嵌套数组带上移位转换,注意按id@(1,id))和子数组的分解(@(2,id).funds[

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "Id": "@(1,Id).k_id",
        "number": "@(1,Id).&",
        "name": "@(1,Id).funds[&1].&",
        "code": "@(1,Id).funds[&1].&",
        "amount": "@(1,Id).funds[&1].balance.k_bal",
        "currency": "@(1,Id).funds[&1].balance.&",
        "othercurrency": "@(1,Id).funds[&1].balance.&",
        "@(0,name)": "@(1,Id).funds[&1].subFunds[&1].name",
        "noOfUnits": "@(1,Id).funds[&1].subFunds[&1].k_units",
        "unitPrice": "@(1,Id).funds[&1].subFunds[&1].&",
        "insurerId": "@(1,Id).&"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=recursivelySquashNulls"
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "k_id": "ONE",
        "number": "ONE",
        "insurerId": "ONE"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": ""
    }
  },
  {
    "operation": "sort",
    "spec": {}
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "funds": {
          "*": {
            "subFunds": {
              "*": {
                "nou": "=divide(1,@(1,k_units))",
                "Tot": "=divide(@(1,unitPrice),@(1,nou))",
                "Total": "=toInteger(@(1,Tot))",
                "SubTotal": "=intSum(@(1,Total),300)"
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": {
              "*": {
                "nou": "",
                "Tot": ""
              }
            }
          }
        }
      }
    }
  }
 ]

其中派生属性TotalSubTotal与最近添加到问题中的重命名属性名称一起添加。

 类似资料:
  • 我有json,其中包括多个产品,每个产品有多个不同的细节。使用jolt,我只需要输入json中的几个字段,遵循与输入json几乎相同的结构。我成功地迭代了产品,但是当我试图迭代每个产品变体时,我没有得到想要的输出。 输入. json 这里是Spec.json 我想要的预期输出。 我现在得到的实际输出。

  • 我有一个JSON如下所示: 为什么在输出中看不到Level1、Level2?请有人帮忙,我想看看在输出和输入太相似了。

  • 所以目前我看到的是这样的: 这是我目前编写的Jolt规范(编辑): 以及转换后的输出: 任何帮助都是非常感谢的。

  • 我只想扁平化嵌套JSON的属性,但仍然适用于输入数组中的所有对象 很难将这三个字段放在一个规范中(类型字段、geo字段、properties字段)。我编写了规范来单独完成每一个操作,但是当我将这些规范组合在一个对象中使用时,它会产生错误的输出--对象数组真的把它搞砸了。 期望输出:

  • 我有一个嵌套的JSON对象,如下所示: 我想将其转换为: 我如何使用JOLT实现这一点?感谢您的参与。

  • 我遇到了一个问题,使用颠簸转换将平面 JSON 转换为嵌套 JSON。而且我对颠簸转型很陌生。输入和输出详细信息如下。 我的输入: 预期产量