【问题标题】:How to pass the dynamic fieldName in filter condition of dataweave mule如何在dataweave mule的过滤条件中传递动态字段名
【发布时间】:2017-09-14 06:12:04
【问题描述】:

我在缓存中有数据,即对象存储。我需要从缓存中获取数据并对其应用过滤条件。

这里的要求是,我必须公开 api 以获取基于字段名称和字段值的数据,这将在请求中动态出现。

样品请求:

https://localhost:8082/api/customer/filter?fieldName=customerId&fieldValue=101810

我有返回代码来过滤 dataweave 中的数据,但它不起作用。你能帮忙吗?

%dw 1.0
%output application/json
---
payload.rows filter (("$.content." ++ flowVars.fieldName ++ ".content") as :string == flowVars.fieldValue as :string) map ((row , indexOfRow) -> {
    customerId: row.content.customerId.content as :string when row.content.customerId.content != null otherwise null,
    customerName: row.content.customerName.content as :string when row.content.customerName.content != null otherwise null,
    customerAddress:row.content.customerAddress.content as :string when row.content.customerAddress.content != null otherwise null
})

我得到以下错误

Exception while executing: 
payload.rows filter (("$.content." ++ flowVars.fieldName ++ ".content") as :string == flowVars.fieldValue as :string) map ((row , indexOfRow) -> {
                      ^
Type mismatch for '++' operator
     found :object, :string
  required :string, :string.

你能帮忙吗

【问题讨论】:

  • 请提供用于在objectstore或flowVars中设置fieldName和fieldValue的代码
  • 我们将它们作为查询参数获取,这些参数将来自像这样的请求 fieldName=customerId&fieldValue=101810
  • 从上面的映射中,我们将获取 fieldName 作为 customerId 或 CustomerName 或 CustomerAddress 来过滤数据,并且 filedVaule 是该字段的对应值。你能帮忙吗
  • 请找到 input(json) as { "rows": [{ "label": "530", "content": { "customerId": { "content": 100002 }, "customerName" : { "content": "John" }, "customerAddress": { "content": "US" } } }, { "label": "530", "content": { "customerId": { "content": 100003 }, "customerName": { "content": "David" }, "customerAddress": { "content": "san diego" } } } ] }

标签: mule mule-studio mule-component mule-el mule-esb


【解决方案1】:

问题在于用于选择器的代码应该类似于$.content[flowVars.fieldName].content。完整的代码将是

%dw 1.0
%output application/json
---
payload.rows filter (($.content[flowVars.fieldName].content) as :string == flowVars.fieldValue as :string) map ((row , indexOfRow) -> {
    customerId: row.content.customerId.content as :string when row.content.customerId.content != null otherwise null,
    customerName: row.content.customerName.content as :string when row.content.customerName.content != null otherwise null,
    customerAddress:row.content.customerAddress.content as :string when row.content.customerAddress.content != null otherwise null
})

这与您提供的输入效果很好。

希望对您有所帮助。

【讨论】:

  • 它工作正常。谢谢@anupambhusari。我们可以在哪里找到与此相关的文档。可以给我链接吗
  • 请参考Dataweave Selector
猜你喜欢
  • 1970-01-01
  • 2017-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多