Alagesan Palani
Alagesan Palani

Reputation: 2014

Parse the json string without quotes into json

Following json string is not converting into json as key is not inside quote.

{file:"http://video.test.com/media/myvideo.mp4", image:"/category/dt/filename.png", width:"100%", height:"100%", stretching:"uniform", autostart:true, modes:[{type:"flash", src:"/swf/external/player.swf"}, {type:"html5"}]}

I have tried:

  1. JSON.parse -- it does not work as keys are not inside quotes.

  2. eval('('+str+')') -- not converting for some reason, also little reluctant for this solution due to security.

  3. Manually insert double quotes delimiting colon (:) but one of my value, which is a url, too has a colon, as given in the solution: regular expression add double quotes around values and keys in javascript

Why is it difficult to convert this string into json and how to convert it?

var s = '{file:"http://video.test.com/media/myvideo.mp4", image:"/category/dt/filename.png", width:"100%", height:"100%", stretching:"uniform", autostart:true, modes:[{type:"flash", src:"/swf/external/player.swf"}, {type:"html5"}]}';

console.log(eval('(' + s + ')'));

Upvotes: 5

Views: 11560

Answers (1)

Ghasan غسان
Ghasan غسان

Reputation: 5857

The main question is really where did you get the string from, but anyways, here is a solution.

var obj = eval('(' + str + ')');
var json = JSON.stringify(obj);

Upvotes: 12

Related Questions