如何使用jolt递归迭代标记

时间:2018-02-21 10:14:48

标签: transformation jolt

我需要使用jolt转换JSON。输入JSON如下:

 {
  "items": [
    {
      "Group1": {
        "ABCCode": "3",
        "ABCDescription": "abcd"
      },
      "Group2": {
        "test2": [
          "123"
        ]
      }
    }
  ]
}

我需要输出如下,

[
   {
      "test2Id": "123",
      "attrname": "ABCCode",
      "attrval": "3"
   },
   {
      "test2Id": "123",
      "attrname": "ABCDescription",
      "attrval": "abcd"
   }
]

如何使用jolt实现这一目标?

1 个答案:

答案 0 :(得分:0)

这将产生给定输入的输出。你可能需要调整它,因为你给它不同类型的输入。

[
  {
    "operation": "shift",
    "spec": {
      "items": {
        "*": {
          "Group1": {
            // match all keys below Group1
            "*": {
              // "$" means grab the key matched above, 
              //   and write it the the output as "attrname"
              "$": "[#2].attrname",
              // "@" means grab the value matched above
              "@": "[#2].attrval",
              // walk back up the match tree three levels
              //  walk back down the path "Group2.test2[0]"
              //  and write that value to the output
              "@(2,Group2.test2[0])": "[#2].test2Id"
            }
          }
        }
      }
    }
  }
]