1

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:

 {
        "pakage": ""packageId":"AIM_PACKAGE", "component":"Handbook",  "fieldName":"Upload Handbook Document","assetUrl":"sflydamlocation.handbookfilename.pdf""
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

1

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.
  2. Take in account that the value of "package" is not really valid JSON either, in case you want to parse it. It should an object (eg " { \"package\":... }")
  3. This script expects all the arrays to have exactly 1 element. 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.

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.

aled
  • 21,330
  • 3
  • 27
  • 34
  • but inside package configuration there will be more than these fields..some times new fields will be added – Karthik Raja J Nov 04 '18 at 13:36
  • any ways to replace those special character like \n \ { } with nospace – Karthik Raja J Nov 04 '18 at 18:26
  • Those are the newlines that help to make the JSON pretty printed however they can be removed with the replace() function. I edited the example to remove them. Do you mean that you want to iterate to any fields with the same pattern? That wasn't mentioned in the original question. Actually Jerney answer seems to do that. – aled Nov 05 '18 at 00:58
  • actually i would like to remove \ and { } so that it would bw perfect – Karthik Raja J Nov 05 '18 at 08:16
  • anyway to replace \ in \"packageId\" into "packageId" – Karthik Raja J Nov 05 '18 at 11:09
  • 1
    unescaping the quotes inside the string value would not be a valid JSON. I strongly not recommend it. If you still want to do that you could use the replace() function and return a Java string instead. – aled Nov 05 '18 at 13:21
0

Something like this should work, unless you require something more flexible. I'm assuming you're working w/ Mule3/DW1:

%dw 1.0
%output application/json

%var packageConfig = payload.packageConfiguration[0]
---
{
  package: packageConfig mapObject ((value, key) -> {
    (key): value[0]
  })
}
jerney
  • 2,187
  • 1
  • 19
  • 31