2

I have this string

{"cb":15,"cl":12,"cr":18,"ct":3,"id":"6yIGexevqu9RGM:","ml":{"366":{"bh":96,"bw":112,"o":0},"454":{"bh":93,"bw":88,"o":0}},"oh":500,"os":"42KB","ou":"http://albums.songspk.link/images/cover/55553137088b0humnava.jpg","ow":500,"pt":"Humnava - Hamari Ahduri Kahani (2015) Download Mp3 Songs ...","rh":"albums.songspk.link","ru":"http://albums.songspk.link/audio-mp3-single-track/humnava_hamar_adhuri_kahani_mp3_song","s":"","th":112,"tu":"https://encrypted-tbn2.gstatic.com/images?q\u003dtbn:ANd9GcTlkE4gq7VQB5vB8-35jIF8YBh5KP14GsquWY1allab1-KnL07cREXXajV0_w","tw":112} 

If you copy and paste it to JSONLINT, you will see it shows an error because of the line break. If I remove the line breaks the error gets resolved. Is there any way I can parse the string to JSON in JavaScript?

10
  • 2
    replace real line break with \n (line break equivalent) Commented Jun 19, 2015 at 16:01
  • 4
    Where does the JSON come from? You should reach out to them and tell them that they produce invalid JSON. The solution is to fix the JSON, not to find ways to parse broken JSON. Commented Jun 19, 2015 at 16:04
  • 2
    You've got two answers telling you to work around it. Don't. Get the owner(s) of the server to fix the JSON. Commented Jun 19, 2015 at 16:11
  • 1
    If you copied it from somewhere, you need to replace real line breaks. Do it in notepad++(with regexp mode enabled, replace \n with \\n) or remove it at all in online services like this textfixer.com/tools/remove-line-breaks.php Commented Jun 19, 2015 at 16:15
  • 1
    I doubt Google would produce invalid JSON, so I'm not sure what you mean by "it's form Google". "i am crawling google for images" sounds like you may be creating the JSON yourself one way or the other. Commented Jun 19, 2015 at 16:29

3 Answers 3

1

I found solution to my problem....

http://juristr.com/blog/2010/05/n-will-break-your-json-jquery-wcf/

In this article... there is this function... for my thing i changed "\n" with "" [blank space] as i only want JSON to be parsed.

FUNCTION IN WEBSITE

function escapeNewLineChars(valueToEscape) { if (valueToEscape != null && valueToEscape != "") { return valueToEscape.replace(/\n/g, "\\n"); } else { return valueToEscape; } } 

WHAT I AM USING NOW

function escapeNewLineChars(valueToEscape) { if (valueToEscape != null && valueToEscape != "") { return valueToEscape.replace(/\n/g, " "); } else { return valueToEscape; } } 

and my problem got solved.... i replace "\n" with " " ...

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

Comments

0

The JSON specification does not mention literal newline as a "char" as part of string values, so you can't just insert in to strings. You can use the escape sequence \n instead.

Comments

0

Bare linebreaks can't occur in JSON strings; you have to escape them. Depending on the way the string is encoded, the proper escape sequence could be \n, \r\n, or \r. That last one is very unlikely these days, but it can happen if you might be dealing with files that were created on pre-OSX Macs.

However it happens, you need to go through the string and make sure you've escaped any linebreaks within it, and then put that into the JSON file. Alternatively, you could remove all linebreaks from the string, but this might not be feasible for your application.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.