Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First, I think you're calling it the wrong thing. "JSON" stands for "JavaScript Object Notation" - it's just a specification for representing some data in a string that explicitly mimics JavaScript object (and array, string, number and boolean) literals. You're trying to build up a JavaScript object dynamically - so the word you're looking for is "object".</p> <p>With that pedantry out of the way, I think that you're asking how to set object and array properties.</p> <pre><code>// make an empty object var myObject = {}; // set the "list1" property to an array of strings myObject.list1 = ['1', '2']; // you can also access properties by string myObject['list2'] = []; // accessing arrays is the same, but the keys are numbers myObject.list2[0] = 'a'; myObject['list2'][1] = 'b'; myObject.list3 = []; // instead of placing properties at specific indices, you // can push them on to the end myObject.list3.push({}); // or unshift them on to the beginning myObject.list3.unshift({}); myObject.list3[0]['key1'] = 'value1'; myObject.list3[1]['key2'] = 'value2'; myObject.not_a_list = '11'; </code></pre> <p>That code will build up the object that you specified in your question (except that I call it myObject instead of myJSON). For more information on accessing properties, I recommend the <a href="https://developer.mozilla.org/en/JavaScript/Guide" rel="noreferrer">Mozilla JavaScript Guide</a> and the book <a href="http://rads.stackoverflow.com/amzn/click/0596517742" rel="noreferrer">JavaScript: The Good Parts</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload