How to create the following type of json array using javascript?
xAxis: {
categories: [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'
]
}
How to create the following type of json array using javascript?
xAxis: {
categories: [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'
]
}
Share
Improve this question
edited Mar 12, 2013 at 8:40
Felix Kling
817k180 gold badges1.1k silver badges1.2k bronze badges
asked Mar 12, 2013 at 8:34
Jetson JohnJetson John
3,8298 gold badges41 silver badges53 bronze badges
10
- This looks more like a JavaScript array, since the values are in single quotes. There are a couple of introductions about arrays out there, and how to convert a JavaScript data type to JSON is covered here: stackoverflow./questions/4162749/…. – Felix Kling Commented Mar 12, 2013 at 8:36
- That already is a JSON array in JavaScript. – mustafa.0x Commented Mar 12, 2013 at 8:38
- 2 @mustafa.0x: No, it isn't. It's a JavaScript array in JavaScript. – T.J. Crowder Commented Mar 12, 2013 at 8:40
- Note that if you have almost any js object, even including quite plex functions/closures, you can use JSON.stringify(). It's worth looking through Crockford's well documented source for stringify(): github./douglascrockford/JSON-js/blob/master/json2.js – Josh Greifer Commented Mar 12, 2013 at 8:44
- 1 @mustafa.0x: No. What is posted is not valid JSON, since the keys and values are not in double quotes and there is no top level element. However even though it is a bit quirky, it is valid JavaScript. JSON is a data-exchange format like XML and can only exist within JavaScript in a string. The syntax of arrays and objects in JSON look similar to array and object literals in JS (in fact they are a subset), but that does not make JS arrays and object literals JSON. – Felix Kling Commented Mar 12, 2013 at 9:27
1 Answer
Reset to default 12Well, you have two options:
Create the array and then stringify it:
var categories = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]; var json = JSON.stringify(categories);
JSON.stringify
exists on most modern browsers, and you can shim it. (There are several shims available, not least from Crockford's github page -- Crockford being the person who defined JSON.)Or just create the JSON string directly:
var json = '["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]';
Re your edit: That's not "an array" anymore, it's an object with an array in it (or an object with an object in it with an array in that). It doesn't fundmentally change the answer, though:
var xAxis = { categories: [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ] };
var json = JSON.stringify(xAxis);
or
var json = '{"categories": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]}';
I wasn't sure whether you wanted the xAxis
layer in there. If so, it's just another layer around the above, e.g.:
var obj = { xAxis: { categories: [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ] } };
var json = JSON.stringify(obj);
or
var json = '{"xAxis": {"categories": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]}}';
More about JSON on the JSON home page. Fundamentally, all strings must be in double (not single) quotes, and all property names must be in double quotes.