如何将json中的key:value对转换为key = ule子中的value [英] How to convert key:value pair in json as key = value in mule

查看:405
本文介绍了如何将json中的key:value对转换为key = ule子中的value的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从JSON文件中形成where子句。我想从下面输入的where位置获取key:value对,并将其转换为key = value

I am trying to form where clause from the JSON file. I want to fetch the key:value pair from the where part of below input and convert it into key = value

Input:

{
   "Table":{
      "TableName":"Employee",
      "Columns":[
         "ID",
         "Name"
      ],
      "Where":{
         "ID":"A-0001",
         "Name":"xyz"
      }
   }
}

预期输出:
我想得到其中ID ='A-0001'和名称='xyz'

我正在使用ule子4.请帮助。

I am using mule 4. Please help.

预先感谢

推荐答案

建筑动态SQL?我建议避免这种情况,并坚持使用参数化查询。

Building dynamic SQL? I would suggest avoiding that and sticking to parameterized queries.

我强烈建议您使用以下路径: https://docs.mulesoft.com/connectors/db/database-connector-examples#use-input-parameters-to-protection -database-queries

I would strongly urge you to use this path: https://docs.mulesoft.com/connectors/db/database-connector-examples#use-input-parameters-to-protect-database-queries

将其参数化非常容易,因为您要做的就是传递 payload .Table.Where 作为您的参数化对象。

It would be really easy to make this parameterized because all you'd have to do is passing in payload.Table.Where as your parameterized object.

如果您要从某种可信任的来源中提取它,并且必须以这种方式进行操作我想您可以忽略column数组,并轻松地做到这一点:

If you're pulling this from some kind of trusted source and have to do things this way I suppose you could ignore the columns array and do it this way pretty easily:

%dw 2.0
output application/json
---
if (payload.Table.Where?)
    "where " ++ ((payload.Table.Where pluck "$($$) = '$($)'") reduce ($$ ++ " and " ++ $))
else
    ""

我敢肯定有更好的方法可以做到这一点,但是我如果您仍然想使用columns数组,也可以这样做。

I'm sure there is a better way to do this, but if you wanted to still use the columns array you could also do this.

%dw 2.0
output application/json
---
"where " ++ (
    payload.Table.Columns reduce ((col, wClause="") -> 
        if (not payload.Table.Where[col]?) wClause
        else wClause ++ (if (wClause != "") " and " else "") ++ ("$(col) = '$(payload.Table.Where[col])'")
    )
)

这篇关于如何将json中的key:value对转换为key = ule子中的value的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆