Actually, the best solution is using JSON:
Documentation
JSON.parse(text[, reviver]);
Examples:
1)
var myobj = JSON.parse('{ "hello":"world" }');
alert(myobj.hello); // 'world'
2)
var myobj = JSON.parse(JSON.stringify({
hello: "world"
});
alert(myobj.hello); // 'world'
3) Passing a function to JSON
var obj = {
hello: "World",
sayHello: (function() {
console.log("I say Hello!");
}).toString()
};
var myobj = JSON.parse(JSON.stringify(obj));
myobj.sayHello = new Function("return ("+myobj.sayHello+")")();
myobj.sayHello();
Answer from Matej on Stack Overflow Top answer 1 of 16
234
Actually, the best solution is using JSON:
Documentation
JSON.parse(text[, reviver]);
Examples:
1)
var myobj = JSON.parse('{ "hello":"world" }');
alert(myobj.hello); // 'world'
2)
var myobj = JSON.parse(JSON.stringify({
hello: "world"
});
alert(myobj.hello); // 'world'
3) Passing a function to JSON
var obj = {
hello: "World",
sayHello: (function() {
console.log("I say Hello!");
}).toString()
};
var myobj = JSON.parse(JSON.stringify(obj));
myobj.sayHello = new Function("return ("+myobj.sayHello+")")();
myobj.sayHello();
2 of 16
95
Your string looks like a JSON string without the curly braces.
This should work then:
obj = eval('({' + str + '})');
WARNING: this introduces significant security holes such as XSS with untrusted data (data that is entered by the users of your application.)
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object › toString
Object.prototype.toString() - JavaScript | MDN
... class Dog { constructor(name, ... code in place, any time an instance of Dog is used in a string context, JavaScript automatically calls the toString() method....
Videos
14:36
Converting Objects to Strings with toString() - Advanced JavaScript ...
06:04
How to Convert a JavaScript Object to a String While ...
16:28
Convert an object to a primitive - Basic JavaScript Fast (20) - ...
07:35
String Casting JavaScript - [Convert a Value to String in JavaScript] ...
01:39
How to Convert a JavaScript Object to a String While Preserving ...
02:45
How to Convert JavaScript Object to JSON String? - YouTube
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String
String - JavaScript | MDN
String primitives and String objects also give different results when using eval(). Primitives passed to eval are treated as source code; String objects are treated as all other objects are, by returning the object. For example:
TutorialsPoint
tutorialspoint.com › home › javascript › javascript strings object
JavaScript Strings Object
September 1, 2008 - The string is a sequence of characters containing 0 or more characters. For example, 'Hello' is a string. JavaScript strings can be created as objects using the String() constructor or as primitives using string literals.
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › JSON › parse
JSON.parse() - JavaScript | MDN
The JSON.parse() static method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.
Top answer 1 of 16
1667
I would recommend using JSON.stringify, which converts the set of the variables in the object to a JSON string.
var obj = {
name: 'myObj'
};
JSON.stringify(obj);
Most modern browsers support this method natively, but for those that don't, you can include a JS version.
2 of 16
182
Use javascript String() function
String(yourobject); //returns [object Object]
or stringify()
JSON.stringify(yourobject)
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › JSON › stringify
JSON.stringify() - JavaScript | MDN
If this is a number, it indicates ... clamped to 10 (that is, any number greater than 10 is treated as if it were 10). Values less than 1 indicate that no space should be used. If this is a string, the string (or the first 10 characters of the string, if it's longer than that) is inserted before every nested object or array. If space is anything other than a string or number (can be either a primitive or a wrapper object) — for example, is null or ...
Techotopia
techotopia.com › index.php › JavaScript_String_Object
JavaScript String Object - Techotopia
JavaScript String object methods and properties are accessed using the standard object dot notation: ... The above example returns the length of the string and assigns it to the variable stringLen.
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-convert-string-to-array-of-objects-javascript
How to Convert String to Array of Objects JavaScript ? - GeeksforGeeks
August 5, 2025 - Another approach to convert a string to an array of objects is to use custom delimiters and object construction. This method is particularly useful when the string format doesn't follow a strict JSON structure but contains well-defined patterns or delimiters. By splitting the string based on these custom delimiters, we can construct objects and create the desired array. Example: The following example demonstrates how to convert a string with custom delimiters into an array of objects.
W3Schools
w3schools.com › js › js_json_parse.asp
W3Schools.com
JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Editor JS Exercises JS Quiz JS Website JS Syllabus JS Study Plan JS Interview Prep JS Bootcamp JS Certificate JS Reference ... A common use of JSON is to exchange data to/from a web server. When receiving data from a web server, the data is always a string. Parse the data with JSON.parse(), and the data becomes a JavaScript object.
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › toString
String.prototype.toString() - JavaScript | MDN
const stringObj = new String("foo"); console.log(stringObj); // Expected output: String { "foo" } console.log(stringObj.toString()); // Expected output: "foo" ... A string representing the specified string value. The String object overrides the toString method of Object; it does not inherit ...
Programiz
programiz.com › javascript › examples › convert-object-string
JavaScript Program to Convert Objects to Strings
// program to convert an object to a string const person = { name: 'Jack', age: 27 } const result1 = String(person); const result2 = String(person['name']); console.log(result1); console.log(result2); console.log(typeof result1); ... In the above example, the String() function converts the ...
O'Reilly
oreilly.com › library › view › learning-javascript › 0596527462 › ch04s03.html
The String Object - Learning JavaScript [Book]
October 17, 2006 - The String object is probably the ... built-in JavaScript objects. A new String object can be explicitly created using the new String constructor, passing the literal string as a parameter: ... The String object has several methods, some associated with working with HTML, and several not. One of the non-HTML-specific methods, concat, takes two strings and returns a result with the second string concatenated onto the first. Example 4-2 demonstrates how to create a String ...
Author Shelley Powers
Published 2006
Pages 352
W3Schools
w3schools.com › js › js_json_stringify.asp
JSON.stringify()
JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Editor JS Exercises JS Quiz JS Website JS Syllabus JS Study Plan JS Interview Prep JS Bootcamp JS Certificate JS Reference ... A common use of JSON is to exchange data to/from a web server. When sending data to a web server, the data has to be a string. You can convert any JavaScript ...