PermaLinkJSON and Domino02:35:48 PM
Written By : Scott Good

Do you do web development? Do you know about JSON? If not, you JSON.gif 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.

Comments :v

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




Enter Comments^


Email addresses provided are not made available on this site.





You can use UUB Code in your posts.

[b]bold[/b]  [i]italic[/i]  [u]underline[/u]  [s]strikethrough[/s]

URL's will be automatically converted to Links


:-) :cry: :-\ :huh: ;-) :cool: :grin: :emb: :laugh: :-p :lips: :-( :rolleyes: :-o :-D :angry: :-x
bold italic underline Strikethrough





Remember me    

Disclaimer & Copyright
Monthly Archive
Contact me...

or IM: JSG1901
SMRT Racing Team

Thank you sponsors!

Infinite Fiberworks Co, a great source for high-quality Porsche fiberglass parts

Infinite Fiberworks Co.

If you are looking for fiberglass for Porsches, IFC is the place to go. I have used parts from most of the major suppliers and IFC's are easily the highest quality and the most reasonably-priced. Contact Mike at Infinite Fiberworks for more information.

Located in Racine, OH (so far Southeast they're almost in West Virginia), IFC's goal is to be The Best. Give 'em a try.


Bent or ugly wheels? Call Wheel Medic!

Wheel Medic & The Round House

Whether you need to repair, repaint, refinish or just replace your wheels, the guys at Wheel Medic/Round House can get you back on the road in no time!

Wheel Medic, Inc is a family-owned company which specializes in the repair and restoration of aluminum wheels.

The Round House was founded to service Wheel Medic's clients looking for more than just repair work...from custom wheel colors to high-end wheel applications and body kits, the Round House is there to serve the discriminating automotive enthusiast.


Used Porsche parts, great prices!

A Part Above

Looking for used parts for 944s, 924s, 968s or other late-model water-cooled Porsches? Contact John at A Part Above.

Located in Strongsville, OH (20 miles south of Cleveland) their goal is to provide top quality parts and services. I can tell you, John is great to work with and the prices? Very hard to beat.


SMRT Motorsports wants you!

SMRT (that's short for Skid Mark Racing Team), a very-

loosely organized band of fun-loving friends who enjoy auto racing (heck, cars in general), and the occasional adult beverage, wants you to be a part of our team.

Go here to find cool T-shirts, sweatshirts, caps and mugs with the SMRT team logo.

The BlogRoll
Speaking engagements, etc.
January 20-24, 2008
Lotusphere 2008 (Orlando, FL)
  • AJAX and JSON for IBM Lotus Domino Applications (JumpStart session)


  • Moving IBM Lotus Notes applications to the web (with Henry Newberry)


  • Implementing AJAX and JSON in Domino Applications (hands-on, with Henry Newberry)


  • A look under the hood at a world-class IBM Lotus Domino Web application (with Henry Newberry)
April 11-13, 2008
Mid-Ohio race course (New Lexington, Ohio) May 15-16, 2008
Mid-Ohio race course (New Lexington, Ohio) May 17-18, 2008
Putnam Park race course (Mount Meridian, IN) June 7-8, 2008
Grattan Park race course (maybe) (Mount Meridian, IN) July 11-13, 2008
Mid-Ohio race course (New Lexington, Ohio) August 15-17, 2008
Mid-Ohio race course (New Lexington, Ohio) September 13-16, 2007
Mid-Ohio race course (New Lexington, Ohio)
  • Racing for the NASA GTS2 National Championship
October 11-12, 2008
Putnam Park race course (Mount Meridian, IN)
Lotus Domino ND6 RSS News Feed RSS Comments Feed Geo URL RSS Validator Blog Admin Lotus Geek Open Notes Picture Database OpenNTF BlogSphere
Calendar
November 2008
Su
Mo
Tu
We
Th
Fr
Sa
1
2
4
5
6
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
By Category
What I do for a living


I am the President of Teamwork Solutions a long-time Lotus, now IBM, Premier Partner.

With offices in Columbus and Cincinnati, Ohio, we specialize in custom application development for Notes, Domino, WebSphere and Workplace. Our software product, ProcessIt! (see below), is quite possibly the world's best, most powerful and easiest-to-use workflow tool for Notes and the web.

Our clients are some of the world's largest corporations along with others that aren't so big.

We do excellent work, quickly, and often on a fixed-fee basis. We'd love to talk to you about your next project.




I am a Contributing Author to Lotus Advisor Magazine, with more than 40 articles under my belt.

I've written how-to series (serieses?) on LotusScript, JavaScript, Cascading Style Sheets (CSS), and now, AJAX (Asynchronous JavaScript and XML), as well as a bit on miscellaneous web development topics.


TheView.jpg

I also write for The View as of the July/August issue where I showed how to take an ugly Notes applications and make it beautiful with just a few minutes' (careful) work.



I am the chief architect and one of two primary developers for what many consider the best all-around workflow tool for Notes/Domino, anywhere, regardless of price.

It's called ProcessIt!, and you can read all about it at www.notesworkflow.com but the bottom line is this: ProcessIt! is fast and easy to learn, extremely powerful, and can be used by mortals. Even--dare I say it?--common users.

You can spend a lot more on a workflow tool but you won't be able to do a lot more for all the extra money.

Don't believe me? Download and try it for free for 60 days.



Copyright Porsche and NASA...not me!

I race a Porsche 944 S2 in National Auto Sport Assocation events and am the 2008 National Champion in NASA's GTS2 class.

Blame this event, a few years ago, for starting that particular money drain all over again.

I'm also a Nationally-Certified Instructor for the Porsche Club of America and am active in teaching high performance driving for them and other enthusiast groups at race track events throughout the Midwest.

In a prior racing life, I was the Midwestern Regional Formula Atlantic Champion and, in 1991, the Ohio Vally Region of SCCA's Regional Driver of the Year (but that, alas, went away when my credit cards let go of the rope!).




I'm writing a book...or at least trying to.

It's murder mystery in which, not too surprisingly, the main character runs a small software company and races cars for fun. Oh yeah, and lives near where I do.

Just where do they come up with these crazy ideas?