将默认值添加到 Jolt 中的数组

Adding default values to array in Jolt

我正在尝试使用 jolt 转换 json,它只是向数组添加一些默认值。

输入如下所示:

{
  "valueA": "A",
  "valueB": "B"
}

输出应该是:

{
  "listWithItems": [ 
    {
      "valA": "A",
      "valB": "B",
      "valC": "C"
    }
  ]
}

我的规格现在看起来是:

[
  {
    "operation": "shift",
    "spec": {
      "valueA": "listWithItems[0].valA",
      "valueB": "listWithItems[0].valB"
    }
  },
  {
    "operation": "default",
    "spec": {
      "listWithItems": [
        {
          "valC": "valC"
        }
      ]
    }
  }
]

我只是无法将 valC 传递给 listWithItems,而且在文档中也没有找到任何内容。有人可以帮我解决这个问题吗?

提前致谢!

一个选项是改变转换的顺序,例如

[
  {
   // Add a default attribute "C" to the current object
    "operation": "default",
    "spec": {
      "valueC": "C"
    }
  },
  {
   // Nest all children of the object under "listWithItems" key name as an array
    "operation": "shift",
    "spec": {
      "*": "listWithItems[0].&"
    }
  }
]

另一个选项使用修改转换如

[
  {
   // Nest all of the current elements of the object under the "listWithItems" array 
    "operation": "shift",
    "spec": {
      "*": "listWithItems[0].&"
    }
  },
  {
   // Add new key-value pair to the array
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": {
          "valueC": "C"
        }
      }
    }
  }
]

您可以查看 https://github.com/bazaarvoice/jolt/releases/ as a source and https://jolt-demo.appspot.com/ 作为游乐场。