如果密钥不存在,则JOLT增值

时间:2018-07-27 11:09:29

标签: key specifications jolt

我的JOLT遇到了一些问题,我会变得直率。

输入

{
  "Root": [{
      "key1": "value1",
      "key2": "value2"
    },
    {
      "key1": "value1"
    }
  ]
}

如您所见,我有一个数组,里面有2个对象。这是我的JOLT:

JOLT

[{
  "operation": "shift",
  "spec": {
    "Root": {
      "*": {
        "key1": "newkey1",
        "key2": "newkey2"
      }
    }
  }
}]

输出显然是这样的:

输出

{
  "newkey1": ["value1", "value1"],
  "newkey2": "value2"
}

现在,我的问题是:当输入json中不存在键时,是否可以在“ newkey2”数组中添加一个空值? 我需要这样的东西:

{
  "newkey1": ["value1", "value1"],
  "newkey2": ["", "value2"]
}
也许这很容易做到,但我刚掌握了JOLT。

1 个答案:

答案 0 :(得分:1)

在转移数据之前先“修复”数据即可。

[
  {
    "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[]"
        }
      }
    }
  }
]