0

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));
5
  • 2
    Escape backslashes Commented Nov 4, 2016 at 11:48
  • I gave you the correct answer and, for some reason, you refuse to test it. I give up... Commented Nov 4, 2016 at 12:24
  • @FDavidov : controlling the behaviour of input string is not in my control. I am getting this string via an API. Can you please suggest an alternate solution? Commented Nov 4, 2016 at 12:46
  • Your input string is OK. Take your example: {"a":JSON.stringify({"Max":"2"})}; to make a JSON object you need to PARSE (JSOM.parse); to print it, you need to STRINGIFY (JSON.strigify). In your example, you are STRIGIFYING something that is already a string and hence the additional back-slashes. If you simply take the example of my answer (test it in your code), you will see that it works perfectly. Commented Nov 4, 2016 at 17:03
  • One more thing: In JavaScript, the single and double quote characters are both treated the same. As such, you can declare a string as: 'this is a string with "double quotes" in the middle' is a perfectly formatted string that saves you from using escaping back-slash character (just before the ") characters within the string. Commented Nov 4, 2016 at 17:06

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Why doesn't JSON.stringifiy output a correctly escaped string?
@PranavCBalan Your solution worked! Thanks. Although I am still facing issues with my actual problem, can you check the edits on the original question.
1

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

3 Comments

I would have done that. Just that the variable s that you have mentioned in your code gets initialized using API request. Can you please check my latest Edit? Many thanks!
the string you have is a stringified json (read string). but why the double stringify?
How else can I parse it?
0

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.

4 Comments

No I am stringify(ing) a JSON object only. Request you to check once again
Request denied!!! The fact that you are getting back-slash chars when printing (the first print) is the proof that I'm right and you are wrong. I suggest you just try my suggestion and, if I'm wrong, I'll publicly request your forgiveness. One additional tip: try to print JSON.strigify(<your variable>,5). You will see a beautiful display of your JSON.
It's not that I want to stringify a JSON which contains stringified JSON. Just that it is a result of an API that I am requesting. And now I have to parse it, can you suggest something?
Read my comment under your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.