我正在尝试使用 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/作为来源和 https://jolt-demo.appspot.com/作为 Playground 。
关于java - 在 Jolt 中向数组添加默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70808743/