Do you do web development? Do you know about JSON? If not, you
should. JSON (pronounced "Jason") is the acronym for JavaScript Object Notation. It is, among other things, a faster, simpler, easier-to-use alternative to XML. And, while that's not all it is, that alone means it is a terrific tool to add to your AJAX toolbox.
So what is it and how can you take advantage of it? I'm glad you asked.
JSON is not something new, but a general awareness of it among the developer community is. What it is, is a smart use of the flexibility of JavaScript variables. Give me a moment to walk you through it.
In JavaScript, there's only one kind of variable. In other words, variables aren't typed. You don't have to declare that variable x is a string while variable y is a number (or an array or an object). Any variable can be any of these things. Whereas in a language like LotusScript you have to declare types with your variables...
Dim ohioStateNum As Integer
Dim ohioStatePop as String
Dim ohioStateArea as String
...in JavaScript you simply identify the variable name:
var ohioStateNum;
var ohioStatePop;
var ohioStateArea;
Or, you may identify variables and give them values at the same time:
var ohioStateNum = 17;
var ohioStatePop = "11.5 million";
var ohioStateArea = "40,948 square miles";
The fact that one is a number and two are strings is unimportant to JavaScript. It's just a variable of some kind.
With arrays, things get a little more interesting. Any variable can be an array. You don't have to do anything different when you declare the variable, you simply have to apply array values to it. You can do that a few different ways. In the olden days (2005, 2004, something like that), arrays were often created like this:
var ohio = new Array(17, "11.5 million", "40,948 square miles");
Anymore, array values are more likely (and preferred) to be defined using the object notation (note the term) format, which is to say with square brackets:
var ohio = [17, "11.5 million", "40,948 square miles"];
Same result, less typing. Note that I've mixed data types here. That's perfectly OK. Once you've declared an array this way you can then retrieve values from it by referencing the array position of the value you want:
var ohioPopulation = ohio[1];
Although that works just fine, it's completely dependent on keeping all the array elements in the right places. Change the sequence of input values and you'll have to rework all the code that references them. It's OK if you're careful about things like that but it would be a heck of a lot better if you could retrieve those values by something that wasn't so place-sensitive.
As it turns out, you can.
If you like, you can assign text strings as your array identifiers, much like you can do with Lists in LotusScript. So, you can do this:
var ohio = [];
ohio["stateNumber"] = 17;
ohio["population"] = "11.5 million";
ohio["landArea"] = "40,948 square miles";
Once you've declared your variables that way, you can access them using the names you've assigned. For instance, you can do this:
var ohioPopulation = ohio.population;
A much easier to read (and for that matter, write) bit of code, it is also completely in-sensitive to sequence. In other words, it matters not a bit what order you use to add elements to your array as their identification is no longer tied to their positions but, rather, to the array elements' names (i.e., "population," "landArea," etc.). You can think of these as being pairs of values: (1) identifiers, like "population," and (2) values, like "11.5 million."
That's a very good thing but it turns out there is yet another format you can use to define these name/value pairs. A format that looks like this:
var ohio = {
stateNumber: 17,
population: "11.5 million",
landArea: "40,948 square miles"
}
That is JSON. Or at least a simple version of it. And, just like with the prior example, values can be extracted from objects defined this way by climbing down the object tree with a standard "dot" notation:
var stateArea = ohio.landArea;
It's very nice and all--and quite useful, and easy to write, and easy to read--but that's not even the cool part.
Remember way back at the beginning when I said one of the nice things about JavaScript variables is they aren't typed and, therefore, can be anything? That applies here, too. So, for instance, the "values" for any of these elements could be another array:
var ohio = {
stateNumber: 17,
population: "11.5 million",
landArea: "40,948 square miles",
areaCodes: [216, 234, 283, 330, 419,
440, 513, 567, 614, 740, 937]
}
Or, the values of one (or more, for that matter) of these named-pairs might themselves be sets of named-pairs:
var ohio = {
stateNumber: 17,
population: "11.5 million",
landArea: "40,948 square miles",
areaCodes: [216, 234, 283, 330, 419,
440, 513, 567, 614, 740, 937],
citySize: {Columbus: 730657, Cleveland: 452208,
Cincinnati: 308728, Toledo: 301285}
}
So, suddenly, if you need to retrieve the population of Columbus, you can go right to it:
var colsPop = ohio.citySize.Columbus;
Easy. Readable. And, something you might actually be able to get right the first time. But wait!--as they say in the ads--there's more! Carrying it another step, this object we've built might be but one of an array of such objects:
var states = {
Ohio: {
stateNumber: 17,
population: "11.5 million",
landArea: "40,948 square miles",
areaCodes: [216, 234, 283, 330, 419,
440, 513, 567, 614, 740, 937],
citySize: {Columbus: 730657, Cleveland: 452208,
Cincinnati: 308728, Toledo: 301285}
},
Oklahoma: {
stateNumber: 46,
population: "3.3 million",
...and so on...
To get the population of Columbus, Ohio from this, you have one more layer:
var colsPop = states.Ohio.citySize.Columbus;
We are finding more and more places to use JSON in our code. I think you'll find the same thing if you take half an hour and play with it a bit. Why bother? I'll give you one great reason: Starting in Domino 7.0.2 (as a more or less undocumented feature), you can not only retrieve Notes View data as XML (with ?ReadViewEntries), but you can also get it back in the JSON format (with ?ReadViewEntries&OutputFormat=JSON). That's pretty big news because the work it takes to parse values out of view data just got a whole lot easier.
I'll write more about getting JSON out of Domino views in a later entry here but, in the meantime, start playing around with this yourself. You'll be glad you did.
1. Mikkel Heisterberg05/11/2007 12:25:59 AM
Homepage: http://lekkimworld.com
Very nice write-up. Thanks!
2. B. Elliott05/11/2007 07:30:45 AM
Best summary I've read yet on JSON. Thanks!
3. David Leedy05/11/2007 08:04:36 AM
Excellent post! Thank you for the great explanation.
Sounds like a great topic for another series of articles for Advisor!
4. Scott Good05/11/2007 09:48:28 AM
Homepage: http://www.scottgood.com
Hi David,
Funny you should say that...I've been thinking about another Advisor series on more advanced JavaScript and/or web development topics, which would include things like JSON.
If you, or anyone else has an opinion on that...or things you'd like to see...I'd be interested in hearing.
Scott
5. Ben Dubuc05/11/2007 12:46:42 PM
Homepage: http://www.benoitdubuc.com
I think you need to demonstrate stuff developers need to do often, like linked keywords, form validation, and other stuff we do in most web applications. A menu driven by JSON might be cool too. Or even a JSON version of your Name Picker!
Just my 2 Canadian cents
Ben
6. Dave Navarre06/06/2007 02:26:04 PM
Homepage: http://www.centretek.net
Excellent. Great explanation - I can imagine someone taking about 20 pages to explain what you did in what amounts to one page. thanks!
7. sushant08/15/2007 08:39:54 AM
what is the based way to parse JSON in lotusscript
8. Scott Good08/15/2007 09:35:28 AM
Homepage: http://www.scottgood.com
JSON is a JavaScript construct. It won't work in LotusScript. OK, I'm sure there's SOME way to parse it out using LotusScript but it would be messy and hard, which is not the point of if.
JSON is for JavaScript (hence the name).
9. Chuck Floyd03/13/2008 11:13:15 AM
Homepage: http://chuckshomeworld.com/
Was browsing the web, reading up on JSON and the like and indeed this is a very good article describing JSON and its "power".
Regarding the comment by sushant, there is an OpenNTF.org project for using JSON in LS.
http://www.openntf.org/Projects/pmt.nsf/ProjectLookup/JSON%20LS
10. Nagarau Sangam03/01/2010 08:32:15 AM
What is the size limit on the JSON object?
How to add elements dynamically to a JSON oject?
























