How to assign JSON string to Javascript variable?

You have not escaped properly. You make sure you do:

var somejson = "{ \"key1\": \"val1\",\"key2\": \"value2\"}";

The easier way would be to just convert an existing object to a string using JSON.stringify(). Would recommend this as much as possible as there is very little chance of making a typo error.

var obj = {
    key1: "val1",
    key2: "value2"
};

var json = JSON.stringify(obj);

If you want the string, not the object (note the ' instead of ")

var somejson =  '{ "key1": "val1", "key2": "value2" }';

If you want a string declared with multiple lines, not the object (newline is meaningful in Javascript)

var somejson =  '{'
 + '"key1": "val1",' 
 + '"key2": "value2"' 
 + '}';

If you want the object, not the string

var somejson =  { "key1": "val1", "key2": "value2" };

If you want a string generically

var somejson =  JSON.stringify(someobject);