Ankit Sharma
Ankit Sharma

Reputation: 47

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));

Upvotes: 0

Views: 73

Answers (3)

Pranav C Balan
Pranav C Balan

Reputation: 115282

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\\"}"}'))
)


UPDATE : If you want to make a string to a valid JSON then use JSON.stringify method.

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":'+JSON.stringify(str)+'}';

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

Upvotes: 2

Nina Scholz
Nina Scholz

Reputation: 386868

Basically you have a \t inside of your JSON. This character is not valid.

With a replacement with a right escaped tab, you could parse the string.

var s = "{\"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\":{}}";

s = s.replace(/\t/g, '\\\\t');
console.log(JSON.parse(s));
console.log(s);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 1

FDavidov
FDavidov

Reputation: 3675

I think you are misinterpreting the concept of JSON and the use of PARSE and STRINGIFY. You DON'T stringify a string as you DON'T parse a JSON object.

In your example, that is exactly what you are doing, you are stringifying a string (I guess you meant to PARSE it instead).

So:

aaa = JSON.parse({"a":JSON.stringify({"Max":"2"})});

will assign a JSON object to aaa, and

console.log(JSON.strigify(aaa)) ;

will print the string version of the JSON.

Upvotes: 0

Related Questions