简体   繁体   English

使用 mule dataweave 将 JSON 数组转换为行

[英]JSON array into line using mule dataweave

I have the below requirement.我有以下要求。

Input is输入是

{ "packageConfiguration": [
      {
        "packageId": [
          "AIM_PACKAGE"
        ],
        "component": [
          "Handbook"
        ],
        "fieldName": [
          "Upload Handbook Document"
        ],
        "assetUrl": [
          "sflydamlocation.handbookfilename.pdf"
        ]
      }
    ]}

I need to convert above json array into this output format:我需要将上面的 json 数组转换为这种输出格式:

 {
        "pakage": ""packageId":"AIM_PACKAGE", "component":"Handbook",  "fieldName":"Upload Handbook Document","assetUrl":"sflydamlocation.handbookfilename.pdf""
}

You can do that treating all fields as strings, however note that:您可以将所有字段视为字符串,但请注意:

  1. The inner quotes must be escaped.必须对内部引号进行转义。 Otherwise the output is not valid JSON.否则输出不是有效的 JSON。
  2. Take in account that the value of "package" is not really valid JSON either, in case you want to parse it.考虑到“包”的值也不是真正有效的 JSON,以防您想解析它。 It should an object (eg " { \\"package\\":... }")它应该是一个对象(例如“ { \\"package\\":... }")
  3. This script expects all the arrays to have exactly 1 element.此脚本期望所有数组都恰好有 1 个元素。 More elements are ignored and less could give an error.更多的元素被忽略,更少的元素可能会出错。 This is not a very robust design.这不是一个非常稳健的设计。

Script (not recommended):脚本(不推荐):

%dw 2.0
output application/json

---
package: using (pc = payload.packageConfiguration[0]) (

        " \"packageId\": \"$(pc.packageId[0])\", " ++  
        " \"component\": \"$(pc.component[0])\" "  ++
        " \"fieldName\": \"$(pc.fieldName[0])\" "  ++
        " \"assetUrl\": \"$(pc.assetUrl[0])\" "
 )

Output:输出:

{
  "package": " \"packageId\": \"AIM_PACKAGE\",  \"component\": \"Handbook\"  \"fieldName\": \"Upload Handbook Document\"  \"assetUrl\": \"sflydamlocation.handbookfilename.pdf\" "
}

This is an ugly string concatenation.这是一个丑陋的字符串连接。 Instead I would suggest to just write the desired output as a JSON object.相反,我建议只将所需的输出编写为 JSON 对象。

Script (recommended):脚本(推荐):

%dw 2.0
output application/dw
var pc = payload.packageConfiguration[0]
---
package: 
    write({
        packageId: pc.packageId[0],  
        component: pc.component[0],  
        fieldName: pc.fieldName[0],  
        assetUrl: pc.assetUrl[0]
        }, "application/json") replace /\n/ with ""

Output输出

{
  "package": "{  \"packageId\": \"AIM_PACKAGE\",  \"component\": \"Handbook\",  \"fieldName\": \"Upload Handbook Document\",  \"assetUrl\": \"sflydamlocation.handbookfilename.pdf\"}"
}

The second script is much cleaner, less error prone and returns an escaped JSON object that you could unescape to use as JSON.第二个脚本更简洁,更不容易出错,并返回一个转义的 JSON 对象,您可以将其转义为 JSON。

Something like this should work, unless you require something more flexible.这样的事情应该可以工作,除非您需要更灵活的东西。 I'm assuming you're working w/ Mule3/DW1:我假设您正在使用 Mule3/DW1:

%dw 1.0
%output application/json

%var packageConfig = payload.packageConfiguration[0]
---
{
  package: packageConfig mapObject ((value, key) -> {
    (key): value[0]
  })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM