JSON.parse(jsonString) is a pure JavaScript approach so long as you can guarantee a reasonably modern browser.
Videos
What is the difference between JSON and a JavaScript object?
Can I customize the indentation?
Is my data sent to any server?
JSON.parse(jsonString) is a pure JavaScript approach so long as you can guarantee a reasonably modern browser.
The jQuery method is now deprecated. Use this method instead:
let jsonObject = JSON.parse(jsonString);
Original answer using deprecated jQuery functionality:
If you're using jQuery just use:
jQuery.parseJSON( jsonString );
It's exactly what you're looking for (see the jQuery documentation).
- Launch Firefox/Chrome/Safari
- Open Firebug/developer tools
- Copy/paste your code into the console.
Then type
console.log(JSON.stringify(object))and voila!{"item1":"value1","item2":1000,"item3":["a","b","c"], "item4":[1,2,3],"item5":{"foo":"bar"}}- Copy/paste back into your text editor.
For more control over the formatting, I have a free online webpage:
http://phrogz.net/JS/NeatJSON
that lets you paste JSON or JS values in one box and see JSON at the bottom, with lots of knobs and sliders to adjust how it looks. For example, the JS value ["foo","bar",{dogs:42,piggies:0,cats:7},{jimmy:[1,2,3,4,5],jammy:3.14159265358979,hot:"pajammy"}] can be formatted like any of the following (and more):
[
"foo", <- adjustable indentation
"bar",
{"dogs":42,"piggies":0,"cats":7}, <- small objects on one line!
{
"jimmy":[1,2,3,4,5], <- small arrays on one line!
"jammy":3.142, <- decimal precision!
"hot":"pajammy"
}
]
[
"foo",
"bar",
{ "cats":7, "dogs":42, "piggies":0 }, <- spaces inside braces!
{
"hot":"pajammy", <- sort object keys!
"jammy":3.14159265358979,
"jimmy":[ 1, 2, 3, 4, 5 ] <- spaces after commas!
}
]
[ "foo", <- 'short' format puts first value
"bar", <- on same line as opening bracket...
{ "dogs" : 42,
"piggies" : 0,
"cats" : 7 }, <- ...and close bracket with last value!
{ "jimmy" : [ 1, 2, 3, 4, 5 ],
"jammy" : 3.14159265358979, <- spaces around colons!
"hot" : "pajammy" } ] <- align object values!

Why wouldn't you just....
...send the result of JSON.stringify(). You don't need to type in the JSON, you need to generate it at runtime if I am not mistaken, so...
var mything = { .... } ;
var jsonRep = JSON.stringify(mything);
See also, Serializing an object to JSON
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));
With JSON.stringify() found in json2.js or native in most modern browsers.
JSON.stringify(value, replacer, space) value any JavaScript value, usually an object or array. replacer an optional parameter that determines how object values are stringified for objects. It can be a function or an array of strings. space an optional parameter that specifies the indentation of nested structures. If it is omitted, the text will be packed without extra whitespace. If it is a number, it will specify the number of spaces to indent at each level. If it is a string (such as "\t" or " "), it contains the characters used to indent at each level.