Ankit Sharma Ankit Sharma - 1 month ago 6
JSON Question

Facing issue while parsing valid JSON string

How to parse this:

JSON.stringify({"a":JSON.stringify({"Max":"2"})});
// prints - {"a":"{\"Max\":\"2\"}"}
JSON.parse(('{"a":"{\"Max\":\"2\"}"}'))
// gives error - unexpected token M


What exactly am I doing wrong?

UPDATE 1:
Pranav's solution worked well. But somehow I am not being able to parse following string :

var str = '{"a":"{\\"PRINT\\":{\\"Maximum Printing Resolution\\":\\"4800 (horizontal)*1 x 600 (vertical) dpi\\",\\"Recommended Printing Area\\":\\"Top margin: 31.6mm Bottom margin: 29.2mm\\",\\"Paper Size\\":\\"A4\\",\\"Paper Handling\\":\\"A4, A5, B5,\\",\\"Paper Weight\\":\\"Plain Paper: 64-105 g/m2\\",\\"Ink End Sensor\\":\\"Dot count\\",\\"Print Head Alignment\\":\\"Manual\\"},\\"SCAN\\":{\\"Scanner Type\\":\\"Flatbad\\",\\"Scanning Method\\":\\"CIS\\",\\"Optical Resolution\\":\\"600 x 1200dpi\\",\\"Selectable Resolution\\":\\"25 - 19200dpi\\",\\"Scanning Bit Depth (Input/Output)\\":\\"16bit / 8bit\\",\\"Line Scanning Speed\\":\\"1.2ms/line (300dpi)\\",\\"Scanning Speed\\":\\"Approx. 19secs\\",\\"Maximum Document Size\\":\\"A4 / Letter (216 x 297mm)\\",\\"Compatible Media\\":\\"Size: A4, Letter Type: Plain Paper\\",\\"Image Quality\\":\\"Draft, Standard\\"},\\"COPY\\":{\\"Copy Speed\\":\\"Approx. 30secs\\",\\"Multiple Copy\\":\\"1 - 21 pages\\"},\\"General\\":{\\"Brand\\":\\"Canon\\",\\"Model Name\\":\\"PIXMA E410\\",\\"Functionality\\":\\"Multi Function\\"},\\"Box Contents\\":{\\"In the Box\\":\\"Power Cable , USB Cable , Manual book with CD & Cartridge\\"},\\"Dimensions\\":{\\"Dimensions - L (Cms)\\":\\"426\\",\\"Dimensions - B (Cms)\\":\\"306\\",\\"Dimensions - H (Cms)\\":\\"145\\"},\\"Warranty Period\\":{\\"Warranty(Years)\\":\\"1\\"},\\"Additional Features\\":{},\\"Compatible Cartridge\\":{}}"}'


UPDATE 2:

Exact code:

var str = "{\"PRINT\":{\"Maximum Printing Resolution\":\"4800 (horizontal)*1 x 600 (vertical) dpi\",\"Recommended Printing Area\":\"Top margin: 31.6mm Bottom margin: 29.2mm\",\"Paper Size\":\"A4\",\"Paper Handling\":\"A4, A5, B5,\",\"Paper Weight\":\"Plain Paper: 64-105 g/m2\",\"Ink End Sensor\":\"Dot count\",\"Print Head Alignment\":\"Manual\"},\"SCAN\":{\"Scanner Type\":\"Flatbad\",\"Scanning Method\":\"CIS\",\"Optical Resolution\":\"600 x 1200dpi\",\"Selectable Resolution\":\"25 - 19200dpi\",\"Scanning Bit Depth (Input/Output)\":\"16bit / 8bit\",\"Line Scanning Speed\":\"1.2ms/line (300dpi)\",\"Scanning Speed\":\"Approx. 19secs\",\"Maximum Document Size\":\"A4 / Letter (216 x 297mm)\",\"Compatible Media\":\"Size:\tA4, Letter Type:\tPlain Paper\",\"Image Quality\":\"Draft, Standard\"},\"COPY\":{\"Copy Speed\":\"Approx. 30secs\",\"Multiple Copy\":\"1 - 21 pages\"},\"General\":{\"Brand\":\"Canon\",\"Model Name\":\"PIXMA E410\",\"Functionality\":\"Multi Function\"},\"Box Contents\":{\"In the Box\":\"Power Cable , USB Cable , Manual book with CD & Cartridge\"},\"Dimensions\":{\"Dimensions - L (Cms)\":\"426\",\"Dimensions - B (Cms)\":\"306\",\"Dimensions - H (Cms)\":\"145\"},\"Warranty Period\":{\"Warranty(Years)\":\"1\"},\"Additional Features\":{},\"Compatible Cartridge\":{}}";

str = '{"a":"'+str.replace(/"/g,'\\\"').replace(/\n/g,'')+'"}';

console.log(str);
console.log(JSON.parse(str));

Answer

You need to escape \ itself by using \ since it has special meaning in a string.

JSON.parse(('{"a":"{\\"Max\\":\\"2\\"}"}'))

console.log(
  JSON.parse(('{"a":"{\\"Max\\":\\"2\\"}"}'))
)

Comments