从m子4中的阵列中删除所有多余的线

发布于 2025-01-20 18:38:29 字数 476 浏览 6 评论 0原文

我想删除多余的行或“\n\r”&来自数组的“\n”,但我的解决方案不起作用。请为此提供正确的函数或数据编织。

输入(json 数组格式):

 [{"m":"a\n\r",
   "a":"b\n"},
  {"m":"a\r\n",
   "a":"b\n"}]

预期输出(json 数组格式):

 [{"m":"a",
   "a":"b"},
  {"m":"a",
   "a":"b"}]

代码:

    %dw 2.0
    var someSpaceJson = write(payload, "application/json", {"indent":false}) 
    output application/json
    ---
    someSpaceJson replace "\n\r" with ""

I want to remove the extra lines or "\n\r" & "\n" from the array but my solution is not working. Please provide the correct function or dataweave for this.

input (json array format):

 [{"m":"a\n\r",
   "a":"b\n"},
  {"m":"a\r\n",
   "a":"b\n"}]

expected output(json array format):

 [{"m":"a",
   "a":"b"},
  {"m":"a",
   "a":"b"}]

code:

    %dw 2.0
    var someSpaceJson = write(payload, "application/json", {"indent":false}) 
    output application/json
    ---
    someSpaceJson replace "\n\r" with ""

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪漫之都 2025-01-27 18:38:29

您需要指定 \\ 而不是 \ 来表示转义字符。

%dw 2.0
var someSpaceJson = write(payload, "application/json", {"indent":false}) 
output application/json
---
read((someSpaceJson replace "\\r" with "" replace "\\n" with ""),"application/json")

这应该会给你你想要的输出。

输入图片此处描述

You need to specify \\ instead of \ to represent the escape char.

%dw 2.0
var someSpaceJson = write(payload, "application/json", {"indent":false}) 
output application/json
---
read((someSpaceJson replace "\\r" with "" replace "\\n" with ""),"application/json")

This should give you your desired output.

enter image description here

流云如水 2025-01-27 18:38:29

如果您想保留值之间的新行并且只想删除尾随的 \r\n,您可以使用以下命令。这也将避免将 JSON 转换为字符串并返回,这通常应该避免。

%dw 2.0
output application/json
---
payload map ($ mapObject ($): trim($))

但是,您需要确保所有值都是 stringnull。如果不是这种情况,您可以在 mapObject 函数本身中添加这些条件。

输入图片此处描述

If you want to preserve the new lines between the values and only want to remove the trailing \r's and \n's you can use the following. This will also avoid converting JSON to string and back which generally should be avoided.

%dw 2.0
output application/json
---
payload map ($ mapObject ($): trim($))

However, you need to make sure that all values are string or null. If that is not the case you can add those conditions in the mapObject function itself.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文