Jolt 删除所有空值

问题描述 投票:0回答:2

我想用Jolt处理器实现JSON转换。我的 JSON 中有空字段,我想删除所有这些字段...

{
  "myValue": 345,
  "colorValue": null,
  "degreeDayValue": null,
  "depthValue": null,
  "distanceValue": null
}

...只保留

myValue
领域。

我可以在移除 Jolt 操作的情况下实现这一点吗?

specifications operation jolt
2个回答
8
投票

要从内部数组中删除

null
值,请在代码末尾使用以下规范:

{
  "operation": "modify-overwrite-beta",
  "spec": {
    "*": "=recursivelySquashNulls"
  }
}

4
投票

有可能,只是不简单。需要两个步骤。

规格::

[
  {
    "operation": "default",
    "spec": {
      // for all keys that have a null value
      //  replace that null value with a placeholder
      "*": "PANTS"
    }
  },
  {
    "operation": "shift",
    "spec": {
      // match all keys
      "*": {
        // if the value of say "colorValue" is PANTS
        //  then, match but do nothing.
        "PANTS": null,
        // otherwise, any other values are ok
        "*": {
          // "recreate" the key and the non-PANTS value
          // Write the value from 2 levels up the tree the "@1"
          //  to the key from 3 levels up the tree => "&2".
          "@1": "&2"
        }
      }
    }
  }
]

出品:

{
  "myValue": 345
}
© www.soinside.com 2019 - 2024. All rights reserved.