6

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'
    ]
}
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Jetson John
  • 3,759
  • 8
  • 39
  • 53
  • This looks more like a JavaScript array, since the values are in single quotes. There are [a couple](http://eloquentjavascript.net/chapter4.html) of [introductions about arrays](https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Predefined_Core_Objects#Array_Object) out there, and how to convert a JavaScript data type to JSON is covered here: http://stackoverflow.com/questions/4162749/convert-js-object-to-json-string. – Felix Kling Mar 12 '13 at 08:36
  • That already is a JSON array in JavaScript. – mustafa.0x Mar 12 '13 at 08:38
  • 2
    @mustafa.0x: No, it isn't. It's a *JavaScript* array in JavaScript. – T.J. Crowder Mar 12 '13 at 08:40
  • Note that if you have almost any js object, even including quite complex functions/closures, you can use JSON.stringify(). It's worth looking through Crockford's well documented source for stringify(): https://github.com/douglascrockford/JSON-js/blob/master/json2.js – Josh Greifer Mar 12 '13 at 08:44
  • @T.J.Crowder It's both: http://www.json.org/ (look at the second item). – mustafa.0x Mar 12 '13 at 09:16
  • 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 Mar 12 '13 at 09:27
  • @FelixKling: You are correct. However, I wasn't specific enough, this is a valid representation of JSON within JavaScript (i.e. if you parsed a JSON object, you could result in the above code, excluding the single-quotes). – mustafa.0x Mar 12 '13 at 09:52
  • @mustafa.0x: No, it isn't. Did you even read what Felix wrote? JSON is a *textual notation for data interchange*. If you have something in JavaScript code that isn't wrapped inside a quoted string, **by definition** it's not JSON. It's a JavaScript initializer. People constantly calling things JSON that aren't is very confusing for beginners and leads to a lot of questions on SO. – T.J. Crowder Mar 12 '13 at 10:24
  • @T.J.Crowder: I did, but apparently no one is reading what I wrote. Is `'["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]'` a valid JSON array? Very well. If we `JSON.parse` that, will we get the array the OP provided? Yes. That is my entire point — the array provided is a valid representation of a JSON array within JavaScript (i.e. it could a pre-parsed JSON array). If you disagree then I have nothing else to say, as we'll then be arguing semantics. – mustafa.0x Mar 12 '13 at 10:54
  • @mustafa.0x: What you've posted in your comment just now is indeed a valid JSON array string. And **not** what's in the question, which you said was JSON, which isn't. I'm done. – T.J. Crowder Mar 12 '13 at 10:57

1 Answers1

12

Well, you have two options:

  1. 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.)

  2. 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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875