如何在数据编织1.0中将json对象转换为小写字母?

问题描述

你好,我有一个json文件作为有效负载,并努力进行转换以转换为小写的元素及其值。

{
 "Name" : "John".
 "e-mails" : ['[email protected]','[email protected]']
}

如果其值中没有数组,则该选项可以像where

但是如何处理数组?

预期输出

{
 "name" : "john".
 "e-mails" : ['[email protected]','[email protected]']
}

有什么建议吗?

解决方法

您需要使用递归函数来覆盖其他类型。

%dw 1.0
%output application/json
%function lowerAll(x)
        x match {
          :object ->  $ mapObject {
                (lower $$): lowerAll($) // assumes all keys are strings
          },:array  ->  $ map lowerAll($),:string -> lower $,default  ->  $
        }  
---
lowerAll(payload)

输入:

{
 "Name" : "John","e-mails" : ["[email protected]","[email protected]"]
}

输出:

{
  "name": "john","e-mails": [
    "[email protected]","[email protected]"
  ]
}