JoltTransform:将输入数组的每个对象拆分为 2 个单独的对象并将其放入输出数组中

问题描述 投票:0回答:1

我正在使用 Jolt Transform 来转换我的输入 JSON,下面给出的是我的示例 JSON 输入 JSON:-

[
  {
    "saleQty": 7,
    "returnQty": 8
  }
]

震动规格:-

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "saleQty": {
          "*": {
            "@1": "[#3].qty",
            "#SALE": "[#3].type"
          }
        },
        "returnQty": {
          "*": {
            "@1": "[#3].qty",
            "#RETURN": "[#3].type"
          }
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "*": {
        "saleQty": {
          "0": ""
        },
        "returnQty": {
          "0": ""
        }
      }
    }
  }
]

实际输出与我的预期结果相符:

[ {
  "qty" : 7,
  "type" : "SALE"
}, {
  "qty" : 8,
  "type" : "RETURN"
} ]

我的问题是,当我尝试在 JSON 数组中提供超过 1 个对象时,相同的 jolt 规范无法在我的输出 JSON 数组中创建新对象。例子, 输入 JSON:

[
  {
    "saleQty": 7,
    "returnQty": 8
  },
  {
    "saleQty": 14,
    "returnQty": 19
  }
]

转换后输出JSON:

[ {
  "qty" : [ 7, 14 ],
  "type" : [ "SALE", "SALE" ]
}, {
  "qty" : [ 8, 19 ],
  "type" : [ "RETURN", "RETURN" ]
} ]

预期输出 JSON:

[ {
  "qty" : 7,
  "type" : "SALE"
}, {
  "qty" : 8,
  "type" : "RETURN"
} ,{
  "qty" : 14,
  "type" : "SALE"
}, {
  "qty" : 19,
  "type" : "RETURN"
}]

有人可以帮我纠正我的 Jolt Spec。我不确定为什么它在我的输出中创建“数量”和“类型”列表。

尝试使用 Jolt Transform 将输入 JSON 转换为预期输出 JSON 树,但我编写的 Jolt 规范没有返回预期输出。需要紧急帮助。

jolt
1个回答
0
投票

您可以使用以下转换:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*Qty": {
          "@": "&2_&(1,1).qty", // separate the layers by &2_&(1,1) : 
          //&2 stands for the uppermost index
          //&(1,1) represents going 1 level up the tree and grabbing the first replacement of asterisk
          "$(0,1)": "&2_&(1,1).type" // bring the key represented by 1st asterisk
            //indeed there's already only one asterisk
        }
      }
    }
  },
  { //get rid of the keys of the objects
    "operation": "shift",
    "spec": {
      "*": "[]"
    }
  },
  { //convert expressions uppercase 
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": "=toUpper"
      }
    }
  }
]
© www.soinside.com 2019 - 2024. All rights reserved.