我对这个 Jolt input 有一些问题,我会尽量直言不讳。
{
"Root": [
{
"key1": "value1",
"key2": "value2"
},
{
"key1": "value1"
}
]
}
如你所见,我有一个数组,里面有 2 个对象。这是我的 Jolt spec :
[{
"operation": "shift",
"spec": {
"Root": {
"*": {
"key1": "newkey1",
"key2": "newkey2"
}
}
}
}]
和输出显然是这样的:
{
"newkey1": ["value1", "value1"],
"newkey2": "value2"
}
现在,我的问题是:当键在输入 JSON 中不存在时,是否可以在
"newkey2"
数组中添加一个空值?
我需要这样的东西:
{
"newkey1": ["value1", "value1"],
"newkey2": ["", "value2"]
}
这也许很容易做到,但我只是掌握了 Jolt 的窍门。
在你轮班之前“修复”数据,让它工作。
[
{
"operation": "modify-default-beta",
"spec": {
"Root": {
"*": {
// Assuming you know the keys
// you expect to be in the data,
// add default values if they don't
// exist.
"key1": "",
"key2": ""
}
}
}
},
{
"operation": "shift",
"spec": {
"Root": {
"*": {
// Now "shift" to the new format
// "knowing" that all the keys will
// have at least default values.
// Also, make "newkey1" always be
// an array.
"key1": "newkey1[]",
"key2": "newkey2[]"
}
}
}
}
]
您可以在 modify
转换规范中使用
~
运算符,根据它的存在使其成为默认值,例如
[
{
"operation": "modify-default-beta",
"spec": {
"Root": {
"*": {
"~key2": "" // if "key2" does not exist or is null, then default it to be ""
}
}
}
},
{
"operation": "shift",
"spec": {
"Root": {
"*": {
"*": "new&" // accumulate values under common keys while prefixing their names with "new"
}
}
}
}
]