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

颠簸变换-将平面映射到层次(嵌套

松雅昶
2023-03-14

我正在尝试使用Jolt将平面结构映射到层次结构http://jolt-demo.appspot.com/#andrewkcarter2:

我的输入是一个包含客户详细信息的平面结构:下面是我的JSON输入:

[
  {
    "customerId": "100",
    "customerName": "Ken",
    "accountId": "1001",
    "accountType": "SAV",
    "transactionAmount": "100.00",
    "homeaddress": "800 W Trade St",
    "businessaddress": "440 S Church St"
  },
  {
    "customerId": "100",
    "customerName": "Ken",
    "accountId": "1001",
    "accountType": "SAV",
    "transactionAmount": "15.00",
    "homeaddress": "800 W Trade St",
    "businessaddress": "440 S Church St"
  },
  {
    "customerId": "100",
    "customerName": "Ken",
    "accountId": "1002",
    "accountType": "CHK",
    "transactionAmount": "200.00",
    "homeaddress": "900 E 4th St",
    "businessaddress": "500 N Church St"
  },
  {
    "customerId": "100",
    "customerName": "Ken",
    "accountId": "1002",
    "accountType": "CHK",
    "transactionAmount": "116.00",
    "homeaddress": "900 E th St",
    "businessaddress": "500 N Church St"
  }
]

这是我的 JOLT 规格:

[
  {
    "operation": "shift",
    "spec": {
    "*": {
    "customerId": {
      "*": {
        "@2": "temp.&1[]"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
    "temp": {
    "*": {
      "0": {
        "customerId": "[#3].customerId",
        "customerName": "[#3].customerName",
        "accountId": "[#3].accounts[0].accountId",
        "accountType": "[#3].accounts[0].accountType",
        "transactionAmount": " 
         [#3].accounts[0].transactions[0].transactionAmount",
        "homeaddress": "[#3].addresses[0].homeaddress",
        "businessaddress": "[#3].addresses[0].businessaddres"
      },
      "*": {
        "accountId": "[#3].accounts[&1].accountId",
        "accountType": "[#3].accounts[&1].accountType",
        "transactionAmount": "[#3].accounts[&1].transactions[&1].&",
        "homeaddress": "[#3].addresses[&1].homeaddress",
        "businessaddress": "[#3].addresses[&1].businessaddress"
       }
      }
    }
   }
 }
]

我得到的结果是:

[ {
  "customerId" : "100",
  "customerName" : "Ken",
  "accounts" : [ {
      "accountId" : "1001",
      "accountType" : "SAV",
      "transactions" : [ {
      "transactionAmount" : "100.00"
    } ]
  }, {
      "accountId" : "1001",
      "accountType" : "SAV",
      "transactions" : [ null, {
      "transactionAmount" : "15.00"
    } ]
  }, {
      "accountId" : "1002",
      "accountType" : "CHK",
      "transactions" : [ null, null, {
      "transactionAmount" : "200.00"
    } ]
   }, {
      "accountId" : "1002",
      "accountType" : "CHK",
      "transactions" : [ null, null, null, {
      "transactionAmount" : "116.00"
      } ]
  } ],
  "addresses" : [ {
     "homeaddress" : "800 W Trade St",
     "businessaddres" : "440 S Church St"
    }, {
     "homeaddress" : "800 W Trade St",
     "businessaddress" : "440 S Church St"
    }, {
     "homeaddress" : "900 E 4th St",
     "businessaddress" : "500 N Church St"
    }, {
     "homeaddress" : "900 E th St",
     "businessaddress" : "500 N Church St"
  } ]
} ]

在上面的输出中,我们看到帐户1001和1002重复交易的次数。我不希望这种情况发生,我希望交易被归类在各自的账户下。

所以,我想要的输出应该是:

[ {
  "customerId" : "100",
  "customerName" : "Ken",
  "accounts" : [ {
    "accountId" : "1001",
    "accountType" : "SAV",
        "transactions" : [ {
              "transactionAmount" : "100.00"
           },
           {
              "transactionAmount" : "15.00"
        } ]
      }, {
    "accountId" : "1002",
    "accountType" : "CHK",
    "transactions" : [ null, null, {
               "transactionAmount" : "200.00"
               },{
              "transactionAmount" : "116.00"
        } ]
      },
     "addresses" : [ {
    "homeaddress" : "800 W Trade St",
    "businessaddres" : "440 S Church St"
  }, {
    "homeaddress" : "900 E th St",
    "businessaddress" : "500 N Church St"
  } ]
} ]

类似的唯一地址只应该出现在结果中。

是否有办法将交易分组到各自的账户下。有人能帮忙吗?

共有1个答案

朱锐
2023-03-14

https://github.com/bazaarvoice/jolt/issues/575回答到这里

请注意,这是一项复杂的任务。对两个不同的输入值进行透视,然后通过管道将数据透视到多个数组中。

这是可能的,但很乱,可能很脆。老实说,我不推荐它。

 类似资料:
  • 我正在尝试为下面的输入编写一个震动转换 - 预期产量为- 我的规格是- 规范没有按照预期的输出进行转换。我想学习如何在字符串解析器中使用属性。

  • 我有这个JSON作为输入: 我需要得到这样的输出: 我需要使用哪种规格? 多谢!

  • 我正在尝试转换以下JSON 用JOLT转换成更简单的东西: 我尝试了很多颠簸的代码,但我不知道如何完成最后一部分。我写了一些颠簸的转变: 但是我不知道如何做最后一部分来旋转name列。name列的值应该基于NAV值旋转。

  • 我正在尝试为以下输入创建震动转换: 所需输出为: 我刚刚开始理解jolt转换的基础,但是对于嵌套结构来说,这似乎有点复杂。

  • 我需要Jolt转换的帮助。我有数组,由许多json对象组成。(https://jolt-demo.appspot.com/) Jolt需要进行哪些转换才能获得以下结果? 现在我有了下一个Jolt构造: 但以下结果不正确:

  • 我正在尝试转换一个包含数组的JSON对象,并将数据卸载为单独的JSON。基本上,我需要为数组的每个元素创建一个对象,并复制所有外部数据。示例: 输入 预期产出 我尝试的规格: 我对 Jolt 很陌生,并尝试了几个不起作用的规格。我在取消嵌套部分时遇到问题。谢谢你的帮助。