Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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
    
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.com/questions/4162749/…. –  Felix Kling Mar 12 '13 at 8:36
    
That already is a JSON array in JavaScript. –  mustafa.0x Mar 12 '13 at 8:38
2  
@mustafa.0x: No, it isn't. It's a JavaScript array in JavaScript. –  T.J. Crowder Mar 12 '13 at 8: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(): github.com/douglascrockford/JSON-js/blob/master/json2.js –  Josh Greifer Mar 12 '13 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 Mar 12 '13 at 9:27

1 Answer 1

up vote 8 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.