如何在 jolt 中使用左右填充来格式化十进制值?

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

我想格式化十进制输入,以便在输出中获得特定数量的字符,小数点前 10 位数字和小数点后 4 位数字。我想用 0 值填充。

例如,如果我接受以下输入:

[
  {
    "field1": "875.45"
  }
]

那么输出应该是:

[
  {
    "field1": "0000000875.4500"
  }
]
java json jolt
1个回答
0
投票

您可以使用 modify-overwrite-beta 转换规范以及 [left/right]Pad 函数,例如

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "pieces": "=split('\\.',@(1,field1))",//separate expression by dot 
        //by using escape( \\ ) characters, since some characters are onsidered 
        //as special such as dot         
        "piece1": "=leftPad(@(1,pieces[0]), 10, '0')",
        "piece2": "=rightPad(@(1,pieces[1]), 4, '0')",
        "field1": "=concat(@(1,piece1),'.',@(1,piece2))"
      }
    }
  },
  {// get rid of the unnecessary attributes
    "operation": "remove",
    "spec": {
      "*": {
        "piece*": ""
      }
    }
  }
]

网站上的演示 https://jolt-demo.appspot.com/ 是:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.