Jolt 将多个对象转换为具有新字段名称的数组

Jolt transform several object to array with new field name

我是 jolt 工具的新手,我想知道是否有办法获取多个 json 对象并将它们放入一个数组中,并使用一个新字段命名如下:

输入 :

{
  "userId": 1,
  "age": 20,
  "desc1": "value desc1",
  "desc2": "value desc2",
  "desc3": "value desc3"
}

JSON 规格 :

[
  {
    "operation": "shift",
    "spec": {
      "userId": "ID",
      "age": "age",
      "*": "additionalInformation"
    }
  }
]

预期结果

{
  "ID": 1,
  "age": 20,
  "additionalInformation": [
    {
      "code": "desc1",
      "value": "value desc1"
    },
    {
      "code": "desc2",
      "value": "value desc2"
    },
    {
      "code": "desc3",
      "value": "value desc3"
    }
  ]
}

使用上面的规范我只能得到这个结果 :

{
  "ID": 1,
  "test": 20,
  "additionalInformation": [
    "value desc1",
    "value desc2",
    "value desc3"
  ]
}

有什么我遗漏的建议吗?

您可以对 code 使用 $@ 通配符value,同时在第一步中以&.为前缀组合公因子下的元素。然后在最后一步将IDage以外的元素合并到同一个数组中,例如

[
  {
    "operation": "shift",
    "spec": {
      "userId": "ID",
      "age": "age",
      "*": {
        "$": "&.code",
        "@": "&.value"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "ID": "&",
      "age": "&",
      "*": "additionalInformation[]"
    }
  }
]

Edit(due to the comment) : 如果你需要选择一些单独的元素,比如 desc1desc3 而不是 all("*"),然后将第一个规范替换为以下

  {
    "operation": "shift",
    "spec": {
      "userId": "ID",
      "age": "age",
      "desc1|desc3": {
        "$": "&.code",
        "@": "&.value"
      }
    }
  }