Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 + ')'));
like image 723
Alagesan Palani Avatar asked Oct 10 '17 03:10

Alagesan Palani


1 Answers

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);
like image 123
Ghasan غسان Avatar answered Nov 05 '22 19:11

Ghasan غسان