jolt变换 - 将平面映射到分层(嵌套

时间:2018-04-24 18:31:55

标签: jolt

我正在尝试使用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 个答案:

答案 0 :(得分:0)

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

请注意,这是一项复杂的任务。透过两个不同的输入值,然后将数据转换成多个数组。

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