HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptCritical

Convert JS object to JSON string

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
stringconvertobjectjson

Problem

If I defined an object in JS with:

var j={"name":"binchen"};


How can I convert the object to JSON? The output string should be:

'{"name":"binchen"}'

Solution

All current browsers have native JSON support built in. So as long as you're not dealing with prehistoric browsers like IE6/7 you can do it just as easily as that:



var j = {
"name": "binchen"
};
console.log(JSON.stringify(j));

Context

Stack Overflow Q#4162749, score: 2060

Revisions (0)

No revisions yet.