mulemule-studiodataweavemulesoft

Need to change csv header from camel case to snake case in dataweave


I am working on a pipeline to dynamically dump all columns from the salesforce object to the S3 bucket. I get all columns for a salesforce object using describe object API. I store all columns into a variable and then create a big SOQL query out of it and submit a bulk query job v2.

Now, this is the main problem. The Column name I am getting from the salesforce connector is in camelCase

 [{
    "Id": 123, 
     "FirstName": "Manual", 
     "MasterRecordId__c" :"abc"
    },
    {   
    "Id": 456, 
     "FirstName": "John", 
     "MasterRecordId__c" :"def"
 }] 

But I want column names to be in snake case

[{ 
   "Id": 123, 
   "first_name": "Manual", 
   "master_record_id__c":"abc"
  },
  {   
   "Id": 456, 
   "first_name": "john", 
   "master_record_id__c":"def"
 }] 

I understand mulesoft has an underscore function to do the same thing, but I am not able to apply any function at "key" level.

Any lead would be really helpful. Please let me know for any questions.


Solution

  • You just have to use mapObject along with the underscore function

    %dw 2.0
    import underscore from dw::core::Strings
    output application/json
    ---
    payload map ((item) -> 
        item mapObject ((value, key) -> {
            (underscore(key)): value       
        })
    )