使用JOLT去除字符串中的转义字符

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

Remove escape characters from string using JOLT

问题

以下是您要翻译的内容:

The output I'm getting:

[
  {
    "MiddleName": "Gisela\\\"mn"
  },
  {
    "MiddleName": "Gisela12"
  }
]

The output I need:

[
  {
    "MiddleName": "Giselamn"
  },
  {
    "MiddleName": "Gisela12"
  }
]
英文:

I'm trying to use replace function to remove the special characters, but the escape characters are not being removed.

Input:

[
  {
    "MiddleName": "Gisela\"mn"
  },
  {
    "MiddleName": "Gisela12---"
  }
]

Spec:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "MiddleName": "=replace('(\\-\\s*)|\\s+--',@(1,MiddleName))"
      }
    }
  }
]

The output I'm getting:

[
  {
    "MiddleName": "Gisela\"mn"
  },
  {
    "MiddleName": "Gisela12"
  }
]

The output I need:

[
  {
    "MiddleName": "Giselamn"
  },
  {
    "MiddleName": "Gisela12"
  }
]

答案1

得分: 1

你可以连续使用splitjoin函数,例如

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": "=split('\"',@(1,&))"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": "=join('',@(1,&))"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": "=split('-',@(1,&))"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": "=join('',@(1,&))"
      }
    }
  }
]
英文:

You can consecutively use split and join functions such as

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": "=split('\"',@(1,&))"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": "=join('',@(1,&))"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": "=split('-',@(1,&))"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": "=join('',@(1,&))"
      }
    }
  }
]

答案2

得分: 1

你可以使用更简单的方式,如下所示的规范:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "MiddleName_temp1": "=split('\"', @(1, MiddleName))",
        "MiddleName_temp2": "=join('', @(1, MiddleName_temp1))",
        "MiddleName_temp3": "=split('-', @(1, MiddleName_temp2))",
        "MiddleName": "=join('', @(1, MiddleName_temp3))"
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "*": {
        "MiddleName*": ""
      }
    }
  }
]
英文:

You can use simpler way like this spec:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "MiddleName_temp1": "=split('\"',@(1,MiddleName))",
        "MiddleName_temp2": "=join('',@(1,MiddleName_temp1))",
        "MiddleName_temp3": "=split('-',@(1,MiddleName_temp2))",
        "MiddleName": "=join('',@(1,MiddleName_temp3))"
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "*": {
        "MiddleName*": ""
      }
    }
  }
]

huangapple
  • 本文由 发表于 2023年3月3日 20:07:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626869.html
匿名

发表评论

匿名网友

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

确定