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:
JSON.parse -- it does not work as keys are not inside quotes.
eval('('+str+')') -- not converting for some reason, also little reluctant for this solution due to security.
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 + ')'));
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With