27

How to apply string object value to a variable Ex.

var str='{a:"www"}'

Now how to set

var obj={a:"www"}

I try eval() but not working

Ankit_Shah55
  • 799
  • 2
  • 9
  • 17
  • Possible duplicate of http://stackoverflow.com/questions/6487167/deserialize-from-json-to-javascript-object – HellaMad Dec 05 '12 at 07:32
  • 1
    possible duplicate of [String to object in JS](http://stackoverflow.com/questions/1086404/string-to-object-in-js) – Peter O. Dec 08 '12 at 20:04
  • possible duplicate of [Safely turning a JSON string into an object](http://stackoverflow.com/questions/45015/safely-turning-a-json-string-into-an-object) – 0lukasz0 Jul 11 '13 at 09:06
  • 1
    If at all possible, you should use valid JSON. eval() is rarely a good idea –  Mar 12 '14 at 18:37

2 Answers2

40

eval should work, and it's actually a MDN solution, not to mention that your string is not a valid JSON, so eval is your only option (if you don't want to include a library for that).

var str='{a:"www"}';
var obj=eval("("+str+")");
console.log(obj);

Quick test in Chrome Dev Tool:

eval("("+'{a:"www"}'+")")
Object
    a: "www"
    __proto__: Object

Just remember to wrap your string in parenthesis and assign it outside eval and it'll be (relatively) safe.

Passerby
  • 9,715
  • 2
  • 33
  • 50
  • 7
    I wasn't wrapping mine in parentheses and I was getting an error. Wrapping the object's string representation in parentheses before calling eval() worked for me too, but why are the parentheses necessary? – Dexygen Dec 27 '15 at 17:58
  • Is there a library to handle this? – BuddhiP Sep 10 '16 at 08:02
  • 1
    @GeorgeJempty - According to the [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval) Those are needed to declare a function, which I guess is the same for an object. If you are passing a string containing a list of statements, it should work. – Pimp Trizkit Apr 29 '17 at 02:57
  • Awesome thank you! This should be the accepted answer – Zachary Raineri Sep 27 '20 at 13:46
27
var str='{"a":"www"}';
var obj = JSON.parse(str);
trebuchet
  • 1,483
  • 9
  • 14
  • 21
    Yes but now it does not corresponds to the requirement :) – Samuel Caillerie Dec 05 '12 at 07:37
  • FYI It should also work with eval. But it is not recommended to use eval for security reasons. – Subir Kumar Sao Dec 05 '12 at 07:38
  • @trebuchet Thanks, why eval is a security risk? – Ankit_Shah55 Dec 05 '12 at 07:58
  • for strings that were stored as JSON_stringify trebuchet solution works. I had string "{"1":["2013-05-10","3","#f70707"]}" and after JSON.parser I got Object {1: Array[3]} – IberoMedia May 10 '13 at 08:17
  • 10
    @Ankit_Shah55 The only time eval becomes a problem is when User1's input is interpreted as javscript on User2. User2 is then taken to a horrible pornography site and his wife walks in. User2 then goes through a terrible divorce and never sees his children again. But that's just one way. – Michael J. Calkins Jul 06 '13 at 21:41