使用JOLT为一个空数组添加默认值

huangapple go评论56阅读模式
英文:

Add default value to an empty array using JOLT

问题

以下是您提供的JSON输入和所需的输出的翻译:

输入JSON:

{
  "my_array": []
}

如果数组为空,我需要向数组中添加一个占位符,所以期望的输出是:

{
  "my_array": ["placeholder"]
}

如果数组有值:

{
  "my_array": ["I have a value", "I am a second value"]
}

我需要保留这些值在数组中,所以期望的输出应该与输入相同:

{
  "my_array": ["I have a value", "I am a second value"]
}

您提到尝试使用默认操作和条件与移动操作,但未成功。如果需要进一步的帮助,请提供更多上下文或详细信息,以便我可以为您提供更多指导。

英文:

I have this JSON as input:

{
  "my_array": []
}

If the array is empty I need to add a placeholder to the array, so this is the desired output:

{
  "my_array": ["placeholder"]
}

If the array has a value/s:

{
  "my_array": ["I have a value", "I am a second value"]
}

I need to keep those values on the array, so the desired output should be the equal to the input:

{
  "my_array": ["I have a value", "I am a second value"]
}

So far I have tried using the default operation without success:

[
  {
    "operation": "default",
    "spec": {
      "my_array[]": [
        "placeholder"
      ]
    }
  }
]

And I have also tried using a conditional with the shift operation without success:

[
  {
    "operation": "shift",
    "spec": {
      "my_array": {
        "[]": {
          "#placeholder": "my_array"
        },
        "*": {
          "@(2,my_array)": "my_array"
        }
      }
    }
  }
]

This last code throws the error:

> Error running the Transform. JOLT Chainr encountered an exception
> constructing Transform className:com.bazaarvoice.jolt.Shiftr at index:0.

答案1

得分: 1

你可以通过测量数组的大小并根据是否等于零来处理此问题,例如:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "sz_array": "=size(@(1,my_array))"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "sz_array": {
        "0": {
          "@2,my_array": { "#placeholder": "my_array[]" }
        },
        "*": {
          "@2,my_array": "my_array"
        }
      }
    }
  }
]

编辑:考虑到您使用的是旧版本的 Jolt 库,您可以选择使用以下规范作为替代方案:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "xyz": ["=toInteger(@(1,my_array))", ["0"]]
    }
  },
  {
    "operation": "shift",
    "spec": {
      "xyz": {
        "*": {
          "0": {
            "@3,my_array": { "#placeholder": "my_array[]" }
          },
          "*": {
            "@3,my_array": "my_array"
          }
        }
      }
    }
  }
]
英文:

You can handle the issue through measuring the size of the array and put into the conditional whether it's equal to zero or not such as

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "sz_array": "=size(@(1,my_array))"
    }
 },
 {
    "operation": "shift",
    "spec": {
      "sz_array": {
        "0": {
          "@2,my_array": { "#placeholder": "my_array[]" }
        },
        "*": {
          "@2,my_array": "my_array"
        }
      }
    }
  }
]

Edit : Considering that you have the older versions of the Jolt library, you can prefer using the following spec as an alternative

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "xyz": ["=toInteger(@(1,my_array))", ["0"]]
    }
  },
  {
    "operation": "shift",
    "spec": {
      "xyz": {
        "*": {
          "0": {
            "@3,my_array": { "#placeholder": "my_array[]" }
          },
          "*": {
            "@3,my_array": "my_array"
          }
        }
      }
    }
  }
]

答案2

得分: 1

The first answer provided by Barbaros Özhan works correctly. But if you have a jolt library version that does not support the size function, this solution should work as well:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "my_array_joined": "=join('',@(1,my_array))"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "my_array_joined": {
        "": {
          "#placeholder": "my_array"
        },
        "*": {
          "@(2,my_array)": "my_array"
        }
      }
    }
  }
]

Edit: The second solution provided by Barbaros Özhan works as well.

英文:

The first answer provided Barbaros Özhan works correctly. But if you have a jolt library version that does not support the size function, this solution should work as well:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "my_array_joined": "=join('',@(1,my_array))"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "my_array_joined": {
        "": {
          "#placeholder": "my_array"
        },
        "*": {
          "@(2,my_array)": "my_array"
        }
      }
    }
  }
]

Edit: The second solution provided by Barbaros Özhan works as well

huangapple
  • 本文由 发表于 2023年5月11日 00:50:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220872.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定