7

So i have some of my example dynamic JSON below, what i'm having trouble doing is escaping everything properly so that it is properly processed by JSON.parse or Jquery.parseJSON, which for some reason it currently isnt. I've tried replacing all quotes but it doesn't solve anything...

var Json = '{"resolved_id":"244296544","resolvedUrl":"http:\/\/www.engadget.com\/2012\/11\/01\/windows-phone-for-mac\/","host":"engadget.com","title":"Windows Phone 7 Connector for Mac updated for WP8, rebranded simply as \'Windows Phone\'","datePublished":"2012-11-01 04:49:00","timePublished":1351763340,"responseCode":"200","excerpt":"For Mac users who prefer Microsoft as their mobile partner, Windows Phone 7 Connector has been the one bridging the divide so far. The sync app has just been updated to v3.0, gaining support for Windows Phone 8 and a concise new name -- \"Windows Phone\" -- to match its Windows 8 counterpart.","authors":{"5437327":{"author_id":"5437327","name":"Deepak Dhingra","url":"http:\/\/www.engadget.com\/editor\/deepak-dhingra"}},"images":{"1":{"item_id":"244296544","image_id":"1","src":"http:\/\/www.blogcdn.com\/www.engadget.com\/media\/2012\/11\/win-phone-for-mac-1351752168.jpg","width":"0","height":"0","credit":"","caption":""}},"videos":"","wordCount":116,"isArticle":1,"isVideo":0,"isIndex":0,"usedFallback":0,"article":"\n<a href=\"http:\/\/www.engadget.com\/2012\/11\/01\/windows-phone-for-mac\/\" nodeIndex=\"493\"><img src=\"http:\/\/www.blogcdn.com\/www.engadget.com\/media\/2012\/11\/win-phone-for-mac-1351752168.jpg\" \/><span class=\"ril_caption\"> <cite><\/cite><\/span><\/a>\n<p nodeIndex=\"91\" scoreAddedToParent=\"37\">For Mac users who prefer Microsoft as their mobile partner, <a href=\"http:\/\/www.engadget.com\/2011\/08\/31\/windows-phone-7-mango-will-play-nicer-with-macs-update-your-con\/\" nodeIndex=\"495\">Windows Phone 7 Connector<\/a> has been the one bridging the divide so far. The sync app has just been updated to v3.0, gaining support for <a href=\"http:\/\/www.engadget.com\/2012\/10\/29\/windows-phone-8-review\/\" nodeIndex=\"496\">Windows Phone 8<\/a> and a concise new name -- \"Windows Phone\" -- to match its <a href=\"http:\/\/www.engadget.com\/2012\/10\/29\/microsft-adds-windows-phone-app-to-windows-store\/\" nodeIndex=\"497\">Windows 8 counterpart<\/a>. The new app plays well with <a href=\"http:\/\/www.engadget.com\/tag\/RetinaMacbookPro\/\" nodeIndex=\"498\">Retina Macs<\/a> too, while other goodies in the changelog include drag-and-drop capability for transferring files in either direction, along with support for iPhoto 9.3.2 and Aperture 3.3.2. Incoming WP8 devices such as the <a href=\"http:\/\/www.engadget.com\/2012\/10\/29\/htc-8x-review-windows-phone-8s-compact-flagship\/\" nodeIndex=\"499\">HTC 8X<\/a> and the <a href=\"http:\/\/www.engadget.com\/2012\/10\/04\/nokia-lumia-920-for-atandt-hands-on-a-windows-phone-8-flagship-wi\/\" nodeIndex=\"500\">Lumia 920<\/a> will also get enhanced ringtone features and allow battery life to be monitored via the app. Persuaded? Then collect your goods at the source link below.<\/p>\n\n"}';
6
  • 1
    How are you creating the json? Are you using JSON.stringify or jquery's $.toJSON ?? Commented Nov 1, 2012 at 15:40
  • How do you know it's not being processed correctly? Commented Nov 1, 2012 at 15:40
  • 1
    Um, you don't need to escape forward slashes / only backslashes `\` Commented Nov 1, 2012 at 15:40
  • 1
    A good place to check your JSON for validity: jsonlint.org Commented Nov 1, 2012 at 15:41
  • @TheZ you do need to escape forward slashes if you are putting the stringified JSON inside a script tag and the JSON contains a string with </script> in it. I've been bit by that one before. Commented Nov 1, 2012 at 15:42

4 Answers 4

5

Inside JSON, quotes within strings need to be escaped with a backslash: {"key": "prop with \" quote"}.

Inside JavaScript, quotes and backslashes within string literals need to be escaped with a backslash: "string with \\backslash and \" quote".

If you really would need to use JSON in JS string literals (there is no reason to do that), you would need to double-escape them: json = "{\"key":\"prop with \\\" quote and \\n linebreak\"}". You haven't done so for the quotes around "Windows Phone".

However, you must've done something wrong when dealing with such problems. Usually you get JSON strings from ajax calls and such, where you get them already as a string value. If you want to echo some sever-created JSON directly into a js script, you don't need to wrap it in a string literal - it is already [nearly] valid Object Literal syntax.

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

5 Comments

Well it's almost what i need, but again it's a complete misunderstanding of my implementation, which is a mistake on my part as i did not mention it. How i am implementing it is that my C# application, will have a webview in it, which will have this Javascript function. And i will call webview.invoke('function','vars'); The 'vars' will be the JSON which i will load from a HTTP Request to an external API.
I'm sorry, I don't know much about C# and its JS abstractions. However, try to echo the JSON without embedding it in quotes
The external API isn't mine :\ so i can't do that.
I'm quite sure that this api does return a valid JSON string, not a half-escaped string literal containing JSON?
yep, this is what i get codepaste.net/6jh4sw But it needs to go into a string before i can do anything, there is no alternative to this.
1

Your problem is probably that your whole Json object is just one string, because of the quotes on the start and end. The idea of JSON is assigning complex variables to one object, like this:

var Json = {
  "resolved_id": "244296544",
  ...
}

Also, there is no need to escape forward slashes.

3 Comments

the problem is i'm getting it in the form of a string. So i really have no alternative.
You ought to tell your supplier that what they are delivering is not JSON at all, but simply a string. And you will need to do something like var Json = eval('{...}'); Note that using eval is quite dangerous and opens your code for all kinds of vulnerabilities.
well i think i've managed to misexplain my predicament. But i managed to solve it using some hacky stuff. So i guess its fine :\
0

All the way at the end:

'...\n\n"}';

Escape the backslashes:

'...\\n\\n"}';

2 Comments

That could be one of the possible incorrect escapes, but currrently even after these fixes, i get an error saying Unexpected token 'W'. Which is referring to the first 'W' after the first \'. I need to know a good way of fixing these escapes so that it works properly :\ And if you try to run the JSON on jsonlint.org it comes out fine...
It is one of the incorrect escapes, amongst others. I've only detected the quote escape before "Windows Phone" on which the parser alerts.
0

According to JSONLint, your problem is on this line:

"title": "Windows Phone 7 Connector for Mac updated for WP8, rebranded simply as \'Windows Phone\'",

If you remove the backslashes on ', it validates. In JSON, you don't escape '

Unfortunately, since you are using ' to delimit your string, you'll need to find another way to escape it. You could use \u0027 in place of \'.

1 Comment

But that won't solve the real problem, which is the \"... The \" needs to be somehow replaced with \\\". I'm unable to write a reliable javascript function which only replace the required quotes to \\\" and not all of the quotes.

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.