cancel
Showing results for 
Search instead for 
Did you mean: 

How to rename a column in a single expression without impacting the other columns in a json file

adityamohanty
New Contributor II

My source data looks like this. I want to rename "Emp.Type" column to "EmpType" in a single expression without impacting the other columns in json file. I don't want to use column map in mapper where we can rename the column name with individual desired column name since this pipeline is generic one which will be used across application. Single expression will help me to pass this expression at run time to get the output

Source Data

--------------------

{
"Body": "New Hire",
"CreatedById": "00553000002Ga35AAC",
"CreatedDate": "2022-10-24T17:09:24.000Z",
"empid": "1234",
"Emp.Type": "Full Time",

}

 

Desired Output

--------------------

{
"Body": "New Hire",
"CreatedById": "00553000002Ga35AAC",
"CreatedDate": "2022-10-24T17:09:24.000Z",
"empid": "1234",
"EmpType": "Full Time",

}

2 ACCEPTED SOLUTIONS

alchemiz
Contributor III

Hi @adityamohanty ,

Good day, you can use mapKeys or string manipulation

** only top level keyname will be updated
$.mapKeys((val,key)=> key == 'Emp.Type' ? 'EmpType' : key)
alchemiz_0-1691857407335.png

** global, replace all matching Emp.Type string to EmpType
JSON.parse(JSON.stringify($).replace(/Emp.Type/gm, m => match m {'Emp.Type'=> 'EmpType'}))

alchemiz_1-1691857640922.png

~EmEm

View solution in original post

adityamohanty
New Contributor II

Thank You @alchemiz. It worked for me

View solution in original post

2 REPLIES 2

alchemiz
Contributor III

Hi @adityamohanty ,

Good day, you can use mapKeys or string manipulation

** only top level keyname will be updated
$.mapKeys((val,key)=> key == 'Emp.Type' ? 'EmpType' : key)
alchemiz_0-1691857407335.png

** global, replace all matching Emp.Type string to EmpType
JSON.parse(JSON.stringify($).replace(/Emp.Type/gm, m => match m {'Emp.Type'=> 'EmpType'}))

alchemiz_1-1691857640922.png

~EmEm

adityamohanty
New Contributor II

Thank You @alchemiz. It worked for me