使用JOLT进行复杂的数组输出

时间:2018-05-13 17:38:13

标签: json jolt

我正在尝试将json转换为复杂数组。 INVLOC的内部数组没有按照我想要的方式生成。

我输入的json是:

    {
  "valid": "true",
  "message": "",
  "data": {
    "ARINVT01": [
      {
        "firstName": "andrew",
        "lastname": "Gilly",
        "INVLOC": {
          "data": [
            {
              "mmm": "MAIN",
              "nnn": "0.000"
            },
            {
              "mmm": "A1",
              "nnn": "0.000"
            }
          ]
        }
      },
      {
        "firstName": "Jack",
        "lastname": "watson",
        "INVLOC": {
          "data": [
            {
              "mmm": "MAIN",
              "nnn": "0.000"
            }
          ]
        }
      }
    ]
  }
}

预期产出:

    [
  {
    "FNAME": "andrew",
    "LNAME": "Gilly",
    "INVBALANCES": [
      {
        "user": "MAIN",
        "CURBAL": "0.000"
      },
      {
        "user": "A1",
        "CURBAL": "0.000"
      }
    ]
  },
  {
    "FNAME": "Jack",
    "LNAME": "watson",
    "INVBALANCES": [
      {
        "user": "A1",
        "CURBAL": "0.000"
      }
    ]
  }
]

我的规格:

  [
  {
    "operation": "shift",
    "spec": {
      "data": {
        "ARINVT01": {
          "*": {
            "firstName": "[&1].FNAME",
            "lastname": "[&1].LNAME",
            "INVLOC": {
              "data": {
                "*": {
                  "mmm": "[&1].INVBALANCES.[#1].user",
                  "nnn": "[&1].INVBALANCES.[#1].CURBAL"
                }
              }
            }
          }
        }
      }
    }
  }

]

获取输出:

    [ {
  "FNAME" : "andrew",
  "LNAME" : "Gilly",
  "INVBALANCES" : [ {
    "user" : "MAIN"
  }, {
    "CURBAL" : "0.000"
  } ]
}, {
  "INVBALANCES" : [ {
    "user" : "A1"
  }, {
    "CURBAL" : "0.000"
  } ],
  "FNAME" : "Jack",
  "LNAME" : "watson"
} ]

有人可以用我的规格帮助我吗?

如果我能够理解JOLT的数组转换,还可以获得某种教程。

1 个答案:

答案 0 :(得分:1)

规格

 [
   {
     "operation": "shift",
     "spec": {
       "data": {
         "ARINVT01": {
           "*": { // array index of ARINVT01
             // 
             //             &0 is "firstName"
             //             &1 is the current array index of ARINVT01
             "firstName": "[&1].FNAME",
             "lastname":  "[&1].LNAME",
             "INVLOC": {
               "data": {
                 "*": { // array index of data array
                   //
                   //       &0 is "mmm"
                   //       &1 is the current array index of the data array
                   //       &2 is "data"
                   //       &3 is "INVLOC"
                   //       &4 is the current array index of ARINVT01
                   //  basically the "&" "number" logic is 
                   //   how many levels to walk back up the document
                   //   to lookup the currently matched value
                   "mmm": "[&4].INVBALANCES.[&1].user",
                   "nnn": "[&4].INVBALANCES.[&1].CURBAL"
                 }
               }
             }
           }
         }
       }
     }
  }
]