Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>See update below</strong></p> <p>If you have a JavaScript object already, use <code>JSON.stringify</code> on it to get the equivalent JSON string. There's an implementation of <code>JSON.stringify</code> in json2.js by Douglas Crockford (originator of JSON) <a href="https://github.com/douglascrockford/JSON-js/" rel="nofollow">on his github page</a>. An increasing number of browsers have it built in now that it's been standardized, but many (many) do not, so for now we still do have to use a utility script (Crockford's or another one).</p> <p>Fundamentally, it's quite easy to generate JSON from an object graph (that was one of the points of JSON, to be something easily produced and consumed).</p> <p>JSON is a subset of JavaScript Object Literal notation. So this JavaScript (not JSON) object:</p> <pre><code>{"bindings": [ {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"}, {"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"} ] } </code></pre> <p>looks <em>exactly</em> like that in JSON. If you were going to put it inside a file you would read into memory or from a server or whatever, that's what you'd put. If you were going to embed a string containing JSON inside JavaScript code (perhaps an odd thing to do, but), you'd just wrap that in quotes:</p> <pre><code>var aJSONString = '{"bindings": [' + '{"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},' + '{"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"}' + ']' + '}'; </code></pre> <p>If you JavaScript literal had been written without quotes around the key names, or using single quotes, you'd have to change that because JSON requires specifically that key names and all strings be in double quotes. So this JavaScript object literal:</p> <pre><code>{foo: 'bar'} </code></pre> <p>becomes</p> <pre><code>{"foo": "bar"} </code></pre> <p>in JSON.</p> <hr> <p><strong>Update</strong>: Re your comment below: Sorry, I misunderstood the question.</p> <p>Building tree structures in JavaScript is dead easy. JavaScript objects are fundamentally fairly freeform collections of key/value pairs (e.g., "maps" or "dictionaries"). So:</p> <pre><code>// Start with a blank object var obj = {}; // Add a leaf to it obj.foo = {}; // Add a leaf under the leaf, with the value 42 obj.foo.subfoo = 42; // Add a new leave to the root; this one's an array with 1, 2, and 3 in it obj.bar = [1, 2, 3]; alert(JSON.stringify(obj)); // '{"foo: {"subfoo": 42}, "bar": [1, 2, 3]}' </code></pre> <p>You can write that as a literal, of course, but I think you know that:</p> <pre><code>var obj = { foo: { subfoo: 42 }, bar: [1, 2, 3] }; </code></pre> <p>What you may not know is that the things on the right-hand side in a literal can be variables; their value will be used, just like with an assignment. So this gives me exactly the same <code>obj</code> as the above:</p> <pre><code>var fortytwo = 42; var obj = { foo: { subfoo: fortytwo }, bar: [1, 2, 3] }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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