使用Mule 4 dataweave删除JSON消息中的所有可能的空格

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

我有一条JSON消息,我需要删除所有格式空间,使值保持不变。在对整个有效负载运行哈希函数之前,这是必需的,因此它必须很精确。

[我从Dataweave编写器配置中的indent=false开始,但是在每个冒号之后都得到了这样的空格:

{"text": "number\": 1 | array\": [ | number\": 1","number": 1,"array": [1,"as",[],{}]}

有人建议用优雅的解决方案删除进入RegEx世界之前剩余的空间吗?如果没有,有没有RegEx解决方案?

json mule dataweave
1个回答
0
投票

我在@SalimKhan(感谢您!)建议的帖子之后得到了这个解决方案。基本上,我只是在DataWeave上编写了完整的JSON自定义编写器。

fun jsonWrite(item) = item match {
    case is Array -> "[" ++ joinBy($ map write($), ",") ++ "]"
    case is Object -> "{" ++ joinBy($ pluck ("\"" ++ $$ ++ "\":" ++ 
        ($ match {
            case is String -> "\"" ++ ($ replace "\"" with "\\\"") ++ "\""
            case is Object -> write($)
            case is Array -> "[" ++ joinBy($ map write($), ",") ++ "]"
            else -> $
        })),",") ++ "}"
    case is String -> "\"" ++ ($ replace "\"" with "\\\"") ++ "\""
    else -> $
}
© www.soinside.com 2019 - 2024. All rights reserved.