从m子4中的阵列中删除所有多余的线
我想删除多余的行或“\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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要指定
\\
而不是\
来表示转义字符。这应该会给你你想要的输出。
You need to specify
\\
instead of\
to represent the escape char.This should give you your desired output.
如果您想保留值之间的新行并且只想删除尾随的
\r
和\n
,您可以使用以下命令。这也将避免将 JSON 转换为字符串并返回,这通常应该避免。但是,您需要确保所有值都是
string
或null
。如果不是这种情况,您可以在 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.However, you need to make sure that all values are
string
ornull
. If that is not the case you can add those conditions in the mapObject function itself.