Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

error while parsing array of objects

0 Kudos

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
1 REPLY 1

geert-janklaps
Active Contributor
0 Kudos

Hi,

I corrected your code:

  • Removed unneeded JSON.stringify statement
  • Removed regular expression
  • Added JSON.parse statement in console.log statement for testing
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