如何在Jolt中进行字符串操作

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

输入:

{
  "Remarks":"COMMENTS:(87) Test Comments"
}

预期输出如下:

{
  "Remarks" : "C(87): TestComments"
  "Id" : 87
}

我想用 C(87): 替换 COMMENTS:(87) 字符串,并且需要在括号中获取 87 并在单独的属性 “Id”中打印相同的内容。

有人可以帮忙吗?

json jolt
1个回答
0
投票

您可以使用 split 函数和 modify 转换,通过左括号和右括号分割字符串,然后使用 concat 函数组合所需的子字符串。在最后一步中,通过使用 remove 转换规范来删除辅助元素,例如

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "str0": "=split('\\(', @(1,Remarks))",
      "str1": "=split('\\)', @(1,str0))",
      "str2": "@(1,str1[1])",
      "Remarks": "=concat('C(',@(1,str2[0]),'):',@(1,str2[1]))",
      "Id": "=toInteger(@(1,str2[0]))"
    }
  },
  {
    "operation": "remove",
    "spec": {
      "str*": ""
    }
  }
]

编辑:以 shift 转换规范开头的替代解决方案是:

[
  {
    "operation": "shift",
    "spec": {
      "Remarks": {
        "*OMMENT*** *": {
          "$(0,1)|$(0,4)|$(0,3)|$(0,5)": "&2"
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "Idd": "=substring(@(1,Remarks[1]),1,3)",
      "Id": "=toInteger(@(1,Idd))",
      "Remarks": "=join(' ',@(1,&))"
    }
  },
  {
    "operation": "remove",
    "spec": {
      "Idd": ""
    }
  }
]
© www.soinside.com 2019 - 2024. All rights reserved.