YosiFZ
YosiFZ

Reputation: 7890

JavaScript JSon string decode

I have this string:

{\"description\": \"PSY - Gangnam Style (\\uac15\\ub0a8\\uc2a4\\ud0c0\\uc77c) \\n\\u25b6 NOW available on iTunes: http:\\\/\\\/Smarturl.it\\\/psygangnam\\n\\u25b6 Official PSY Online Store US \\u0026 International : http:\\\/\\\/psy.shop.bravadousa.com\\\/\\n\\u25b6 About PSY from YG Ent.: http:\\\/\\\/smarturl.it\\\/YGfamilyAboutPSY\\n\\u25b6 PSY's Products on eBay: http:\\\/\\\/stores.ebay.com\\\/ygentertainment\\n\\u25b6 YG-eShop: http:\\\/\\\/www.ygeshop.com\\n \\n===============================\\nPSY CONCERT \\\"HAPPENING\\\"\\n2013.4.13. SAT 6:30PM\\nTHE SEOUL WORLD CUP STADIUM\\nYouTube LIVE@ http:\\\/\\\/www.youtube.com\\\/officialpsy\\nTickets: http:\\\/\\\/smarturl.it\\\/PsyHappeningKor\\nEnglish Booking: http:\\\/\\\/smarturl.it\\\/PsyHappeningEng\\n===============================\\n\\nFor More Information @\\nhttp:\\\/\\\/www.facebook.com\\\/officialpsy\\nhttp:\\\/\\\/twitter.com\\\/psy_oppa\\nhttp:\\\/\\\/twitter.com\\\/ygent_official\\nhttp:\\\/\\\/me2day.net\\\/psyfive\\nhttp:\\\/\\\/www.psypark.com\\nApp Store: http:\\\/\\\/goo.gl\\\/l9TU6\\nGoogle Play: http:\\\/\\\/goo.gl\\\/UiEn1\\n\\n\\u00a9 YG Entertainment Inc. All rights reserved.\"}

And i want to parse it with Json and it's fail. i try to convert the \" with " with :

tmp = tmp.replace(/\\"/gi, '"'); 

And it change it to :

{"description": "PSY - Gangnam Style (\\uac15\\ub0a8\\uc2a4\\ud0c0\\uc77c) \\n\\u25b6 NOW available on iTunes: http:\\\/\\\/Smarturl.it\\\/psygangnam\\n\\u25b6 Official PSY Online Store US \\u0026 International : http:\\\/\\\/psy.shop.bravadousa.com\\\/\\n\\u25b6 About PSY from YG Ent.: http:\\\/\\\/smarturl.it\\\/YGfamilyAboutPSY\\n\\u25b6 PSY's Products on eBay: http:\\\/\\\/stores.ebay.com\\\/ygentertainment\\n\\u25b6 YG-eShop: http:\\\/\\\/www.ygeshop.com\\n \\n===============================\\nPSY CONCERT \\"HAPPENING\\"\\n2013.4.13. SAT 6:30PM\\nTHE SEOUL WORLD CUP STADIUM\\nYouTube LIVE@ http:\\\/\\\/www.youtube.com\\\/officialpsy\\nTickets: http:\\\/\\\/smarturl.it\\\/PsyHappeningKor\\nEnglish Booking: http:\\\/\\\/smarturl.it\\\/PsyHappeningEng\\n===============================\\n\\nFor More Information @\\nhttp:\\\/\\\/www.facebook.com\\\/officialpsy\\nhttp:\\\/\\\/twitter.com\\\/psy_oppa\\nhttp:\\\/\\\/twitter.com\\\/ygent_official\\nhttp:\\\/\\\/me2day.net\\\/psyfive\\nhttp:\\\/\\\/www.psypark.com\\nApp Store: http:\\\/\\\/goo.gl\\\/l9TU6\\nGoogle Play: http:\\\/\\\/goo.gl\\\/UiEn1\\n\\n\\u00a9 YG Entertainment Inc. All rights reserved."}

But here i get :

PSY CONCERT \\"HAPPENING\\"\\n2013.4.13. SAT 6:30PM

In one of the rows and i want to know if there is any decode method to decode the string to be valid for parse to json object?

Upvotes: 0

Views: 257

Answers (3)

Aryeh Leib Taurog
Aryeh Leib Taurog

Reputation: 5598

Not as elegant as basilikum's approach, and likely also not as correct as the double-parsing approach that Bergi and Jonathan Lonowsky suggest, but you could use a Javascript negative lookbehind equivalent, along these lines:

str.replace(/\\+"/gi, function(s){return (s.length % 2) ? s : s.slice(0,-2) + '"';})

This is definitely more secure than evaluating the string.

Upvotes: 0

Bergi
Bergi

Reputation: 664207

Your string seems to be double-escaped but missing its outer delimiters.

var str = '{\\"description\\": \\"PSY - Gangnam Style (\\\\uac15\\\\ub0a8\\\\uc2a4\\\\ud0c0\\\\uc77c) … .\\"}';
var jsonStr = JSON.parse('"'+str+'"'),
    obj = JSON.parse(jsonStr);

Upvotes: 1

basilikum
basilikum

Reputation: 10528

It might be not the best solution, but evaluating your string before you parse it to JSON should work:

eval("tmp = \"" + tmp + "\";");
var tmpJSON = JSON.parse(tmp);

But keep in mind that you are evaluation a presumably unknown string, which is not really safe. Maybe there is a decoding function out there, that does the same.

So why should this work?

You string looks like a string, that I would write when I write a piece of code. It has all the escape backslashes that are needed to print a quote or a backslash itself. But those escape characters shouldn't be really inside the string, they are just there to define what the string should look like. So, writing var str = "He said \"ok\""; will produce a string like He said "ok". But since your string actually contains the escape characters, it would be defined like this instead:

var str = "He said \\\"ok\\\"";

which will produce a string like this:

He said \"ok\"

Now everything inside of eval is interpreted as a piece of code. So when I write:

eval("str = \"" + str + "\";");

the line of code that gets executed is:

str = "He said \"ok\"";

which results in the desired unescaped string:

He said "ok"

Upvotes: 0

Related Questions