Dataweave Append in Transform

123 views Asked by At

I am trying to concatenate all the id values present in "fields" Array in a single variable.

Input:

{
    "rootId":"1oV_wWX-19utijBTxn1QHLsVFjvO-8DSG",  
    
    "jobName":"C##-#9-00057 Demand QA 1.1",
    "folderName":"Billing",
     "parentId":"188Hde_c_ZQiFQeSZX7IxCJGSWySWFOHn",
     "includeSubfolder":true,
    "files": [{ 
        "id": "1JdVXF4EDI2if6kxTSTsYlYhmADeXZQf3",
        "name":"Billing",
        "mimeType": "application/vnd.google-apps.folder"
         },
        { 
        "id": "1KeYyslxo4ezNeuEdN4XrXrWvfaaKKvXU",
        "name":"GoogleSheet1",
      "mimeType": "application/vnd.google-apps.document"
         },
         { 
        "id": "1Poh5FMry692_0iFfmdcwtqT6RxeeoOt0",
        "name":"Forms",
        "mimeType": "application/vnd.google-apps.folder"
         },
          { 
        "id": "1NFQ_89UzVlLYduquZ4hsch4dB65h6fkv",
        "name":"Zipfile.zip",
        "mimeType": "application/zip"
         }
     ]
}

The above is my input payload.

I am trying below Dataweave expression

%dw 2.0
output plain/text
var FileorFolderIds = ""
---
payload.files map (Sample, indexOfSamples) ->{
    FileorFolderIds = FileorFolderIds ++ ',' ++ Sample.id
}

but giving me error.

Expected output:

FileorFolderIds = 1JdVXF4EDI2if6kxTSTsYlYhmADeXZQf3, 1KeYyslxo4ezNeuEdN4XrXrWvfaaKKvXU, 1Poh5FMry692_0iFfmdcwtqT6RxeeoOt0, 1NFQ_89UzVlLYduquZ4hsch4dB65h6fkv

like the above

2

There are 2 answers

0
maddestroyer7 On

@Count Boxer is close but doesn't quite have expected output and I believe it can be further simplified.


%dw 2.0
output text/plain
---
"FileorFolderIds = $(payload.files.*id joinBy  ',')"
0
Count Boxer On
%dw 2.0
output text/plain
var FileorFolderIds = ""
---
payload.files map $.id joinBy  (",")

This returns:

1JdVXF4EDI2if6kxTSTsYlYhmADeXZQf3,1KeYyslxo4ezNeuEdN4XrXrWvfaaKKvXU,1Poh5FMry692_0iFfmdcwtqT6RxeeoOt0,1NFQ_89UzVlLYduquZ4hsch4dB65h6fkv