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""
}

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

but inside package configuration there will be more than these fields..some times new fields will be added
any ways to replace those special character like \n \ { } with nospace
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.
actually i would like to remove \ and { } so that it would bw perfect
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.
|
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]
  })
}

4 Comments

but inside package configuration new fields may add so is there any way to line it up
For sure, will modify my answer
"package": { "packageId": "AIM_PACKAGE", "component": "Handbook", "fieldName": "Upload Handbook Document", "assetUrl": "sflydamlocation.handbookfilename.pdf" } just need to remove those { and } the it will be fine
You can probably use some combination of this answer and the answer above. Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.