2021 Jan 12 12:20 PM
here is my array values when i try to to parse final output it is throwing the erro
var Data=[
{
"COL_1": "USD",
"COL_2": "USD",
"COL_3": null,
"COL_4": "51887559",
"COL_5": "25203392.21",
"COL_6": "0",
"COL_7": "00:00.0",
"COL_8": "LIVE",
"COL_9": "LN_BR",
"COL_10": "USD",
"COL_11": "51887559.21",
"COL_12": "Treasury Markets - Hedges",
"COL_13": "238",
"COL_14": "MX2-HK-43434778|HK_ALM_LNBR|0",
"COL_15": "PCT2-XX-63116",
"COL_16": "-26684167"
}];
Data=JSON.stringify(Data);
data=JSON.stringify(data);
var arr=[
{
"COL_1": "TransCy",
"COL_2": "NotionalCurrency1",
"COL_3": "NotionalCurrency2",
"COL_4": "FPSLNotionalInTransCurrency",
"COL_5": "FSDPNotionalLeg1",
"COL_6": "FSDPNotionalLeg2",
"COL_7": "CoB",
"COL_8": "LifecycleStatus",
"COL_9": "ProductGroup",
"COL_10": "GrpCy",
"COL_11": "FPSLNotionalInGrpCurrency",
"COL_12": "PnLGroup",
"COL_13": "BUCode",
"COL_14": "Contract",
"COL_15": "CollectionID",
"COL_16": "DifferenceOfNotionalAmts"
}
];
var reg="";
for (var col in arr[0]) {
reg = new RegExp(`"${col}":`, "g");
Data = Data.replace(reg, arr[0][col]);
}
when i try to do parsing using
JSON.parse(Data);
it is throwing error like
SyntaxError: Unexpected token T in JSON at position 2
2021 Jan 12 9:28 PM
Hi,
I corrected your code:
var Data=[{
"COL_1": "USD",
"COL_2": "USD",
"COL_3": null,
"COL_4": "51887559",
"COL_5": "25203392.21",
"COL_6": "0",
"COL_7": "00:00.0",
"COL_8": "LIVE",
"COL_9": "LN_BR",
"COL_10": "USD",
"COL_11": "51887559.21",
"COL_12": "Treasury Markets - Hedges",
"COL_13": "238",
"COL_14": "MX2-HK-43434778|HK_ALM_LNBR|0",
"COL_15": "PCT2-XX-63116",
"COL_16": "-26684167" }];
Data=JSON.stringify(Data);
var arr=[{
"COL_1": "TransCy",
"COL_2": "NotionalCurrency1",
"COL_3": "NotionalCurrency2",
"COL_4": "FPSLNotionalInTransCurrency",
"COL_5": "FSDPNotionalLeg1",
"COL_6": "FSDPNotionalLeg2",
"COL_7": "CoB",
"COL_8": "LifecycleStatus",
"COL_9": "ProductGroup",
"COL_10": "GrpCy",
"COL_11": "FPSLNotionalInGrpCurrency",
"COL_12": "PnLGroup",
"COL_13": "BUCode",
"COL_14": "Contract",
"COL_15": "CollectionID",
"COL_16": "DifferenceOfNotionalAmts"
}];
for (var col in arr[0]) {
Data = Data.replace(col, arr[0][col]);
}
console.log(JSON.parse(Data));
I tested this code in JSFiddle (https://jsfiddle.net/xuyLd6n9/6/):
Best regards,
Geert-Jan Klaps