loSerializer = CREATEOBJECT("wwJsonSerializer") lcJSON = loSerializer.Serialize("Hello Rick")
returns: "Hello Rick"
loSerializer = CREATEOBJECT("wwJsonSerializer") lcJSON = loSerializer.Serialize(DateTime())
Notice it's an object declaration that gets returned - JSON is essentially a snippet of JavaScript code that JavaScript can execute so here a new date object is created (and yes this is part of the JSON spec). Simple types are easy, so a bool returns true or false, and a number returns 10 or 101.21 without any quotes. These are literal values just as they are in JavaScript.
loCustomer = CREATEOBJECT("EMPTY") ADDPROPERTY(loCustomer,"Company","West Wind Technologies") ADDPROPERTY(loCustomer,"Name","Rick Strahl") ADDPROPERTY(loCustomer,"Entered",DATETIME()) loAddress = CREATEOBJECT("EMPTY") ADDPROPERTY(loAddress,"Street","32 Kaiea") ADDPROPERTY(loAddress,"City","Paia") ADDPROPERTY(loAddress,"State","HI") ADDPROPERTY(loAddress,"Zip","96779") ADDPROPERTY(loAddress,"Occupants",1) ADDPROPERTY(loCustomer,"Address",loAddress) loSerializer = CREATEOBJECT("wwJsonSerializer") lcJSON = loSerializer.Serialize(loCustomer)
which yields:
{"address":{"city":"Paia","occupants":1,"state":"HI","street":"32 Kaiea","zip":"96779"}, "company":"West Wind Technologies", "entered":new Date("Tue, 3 Oct 2006 21:06:33 -1000"),"name":"Rick Strahl"}
All properties are created inside of JavaScript object notation ({ }) and an inner object is created for the address. As you can see JSON is self-describing in that there's no schema required - the format lets JavaScript know what type you're dealing with.
Note that all properties are rendered in lower case!
SELECT company,careof, entered ; FROM tt_cust ; into cursor TCustomers ; order by Company loSerializer = CREATEOBJECT("wwJsonSerializer") lcJSON = loSerializer.Serialize("cursor:TCustomers")
The result looks like this:
{"Rows":[{"company":"Avionics Inc. asdasd","careof":"John Doe","entered":new Date("Wed, 16 Mar 2005 00:00:00 -1000")},{"company":"Avis World Headquarters","careof":"Steve Herbin","entered":new Date("Tue, 14 Jun 2005 00:00:00 -1000")},{"company":"BGP Productions","careof":"German Vicencio","entered":new Date("Mon, 4 Jul 2005 00:00:00 -1000")}]}
It basically creates an array of Rows of objects one for each row in the table. Each object then contains properties for each of the fields in the table in lower case.
var ResultValue = evaluate(' + jsonResultString + ');
which is vastly more efficient than any XML parsing would be. In addition JSON is significantly more compact than XML. It's a win win situation.