I have a json object that's generated from a csv file:
{"FID":["FID"],"the_geom":["the_geom"],"X":["X"],"Y":["Y"],"ROW":["ROW"],"COL":["COL"],"EMH":["EMH"],"B":["B_SURF","B_SUB1","B_SUB2"],"BP_CA":["BP_CA_SUR","BP_CA_SUB1","BP_CA_SUB2"],"BP_K":["BP_K_SURF","BP_K_SUB1","BP_K_SUB2"],"BP_MG"["BP_MG_SUR","BP_MG_SUB1","BP_MG_SUB2"],"BP_NA":["BP_NA_SUR","BP_NA_SUB1","BP_NA_SUB2"],"CA"["CA_SURF","CA_SUB1","CA_SUB2"],"CAMG":["CAMG_SURF","CAMG_SUB1","CAMG_SUB2"],"CEC"["CEC_SURF","CEC_SUB1","CEC_SUB2"],"CLAY":["CLAY_SURF","CLAY_SUB1","CLAY_SUB2"],"CU"["CU_SURF","CU_SUB1","CU_SUB2"],"DPRR_SUMM":["DPRR_SUMM"],"DRAIN_SUMM"["DRAIN_SUMM"],"FE":["FE_SURF","FE_SUB1","FE_SUB2"],"K"["K_SURF","K_SUB1","K_SUB2"],"KMG":["KMG_SURF","KMG_SUB1","KMG_SUB2"],"MG"["MG_SURF","MG_SUB1","MG_SUB2"],"MN":["MN_SURF","MN_SUB1","MN_SUB2"],"NA":["NA_SURF","NA_SUB1","NA_SUB2"],"OM":["OM_SURF","OM_SUB1","OM_SUB2"],"P"["P_SURF","P_SUB1","P_SUB2"],"PAVL"["PAVL_SURF","PAVL_SUB1","PAVL_SUB2"],"PAW24_SUMM":["PAW24_SUMM"],"PAW30_SUMM"["PAW30_SUMM"],"PAW36_SUMM":["PAW36_SUMM"],"PAW42_SUMM":["PAW42_SUMM"],"PAW48_SUMM":["PAW48_SUMM"],"PH":["PH_SURF","PH_SUB1","PH_SUB2"],"RZFC_SUMM":["RZFC_SUMM"],"RZKUSAT_SU"["RZKUSAT_SU"],"RZPAW_SUMM":["RZPAW_SUMM"],"RZPWP_SUMM"["RZPWP_SUMM"],"RZSAT_SUMM":["RZSAT_SUMM"],"S":["S_SURF","S_SUB1","S_SUB2"],"SAND":["SAND_SURF","SAND_SUB1","SAND_SUB2"],"TEX"["TEX_SURF","TEX_SUB1","TEX_SUB2"],"THICK"["THICK_SURF","THICK_SUB1","THICK_SUB2"],"TIP":["TIP_SURF","TIP_SUB1","TIP_SUB2"],"ZN":["ZN_SURF","ZN_SUB1","ZN_SUB2"],"EMV"["EMV"],"ASP":["ASP"],"DEM":["DEM"],"SLOPE\r":["SLOPE\r"]}
At the very end, the slope object has a carriage return in the name (it was carried over from the csv conversion)
I need to remove that carriage return from the object, as it messes up some other scripts that parse the object.
I tried testing it by doing:
console.log("Without carrage return: " + JSON.stringify(jsonObject).replace(/[\n\r]/g, ''));
But when I print it, it still contains the carriage return.
I'm probably doing something stupid here, I'm just so tired I'm not noticing it.
Any help is appreciated, thanks!
Your regex is a little bit broken. Since \ is also a special character you need to defferentiate that as well.
Try doing this instead:
console.log("Without carrage return: " + JSON.stringify(jsonObject).replace(/[\\n\\r]/g, ''));
Your regex is matching the \r character as a whole but since you are converting everything to a string you end up having to match for the characters '\' and 'r'.
In regular expressions \r and \n match carriage return and new line, but JSON.stringify() transforms the carriage return into '\r'.
So you just need to use \\r instead of \r:
console.log("Without carrage return: " + JSON.stringify(jsonObject).replace(/\\n|\\r/g, ''));
EDIT: /[\\n\\r]/g would have matched every backslash and every letter r and n in the string and not only the sequence \r. /\\n|\\r/g works better.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With