0

I'm having a problem in converting an object

var obj={"Id":"1-AQC1Y1S","Root Order Item Id":"1-AQC1RSA","SC Long Description":"6.5" TXL/Qn/"};

In the above object, we have a value like 6.5" in a string. Please help me out here. Thanks in advance.

2 Answers2

2

Just escape it as follow: 6.5\"

Once again, that's not an array, is an object instead

var obj={"Id":"1-AQC1Y1S","Root Order Item Id":"1-AQC1RSA","SC Long Description":"6.5\" TXL/Qn/"};
console.log(obj["SC Long Description"])

Assuming that's the fixed structure, you can capture the the groups using a regexp, and make a replacement when the group "SC Long Description" is found:

var str = '{"Id":"1-AQC1Y1S","Root Order Item Id":"1-AQC1RSA","SC Long Description":"6.5" TXL/Qn/"}'

var found = false;
str = str.replace(/(".*?")(?!\})/g, function(match) {
  if (found && match.endsWith('"')) return match.substring(0, match.length - 1) + '\\"';
  found = found || match === '"SC Long Description"';
  
  return match;
});

var obj = JSON.parse(str);
console.log(obj["SC Long Description"]);
Ele
  • 33,468
  • 7
  • 37
  • 75
0

You need to escape the double quotes, simple.

var array1 = {
  "Id": "1-AQC1Y1S",
  "Root Order Item Id": "1-AQC1RSA",
  "SC Long Description": "6.5\" TXL/Qn/"
};
console.log(array1["SC Long Description"]);
Kunal Mukherjee
  • 5,775
  • 3
  • 25
  • 53