JavaScript Object Notation (JSON) is a language for describing data, similar in principle to XML, but more concise. It's a handy way for apps to pass data back and forth, and is widely used on the Web. It includes support for arrays with numbered elements, and for structures with named elements, much like Miva Script.
Here are a couple of Miva Script functions for converting between JSON and Miva Script variables. Both functions will work with any type of data: arrays of structures, structures with multiple levels of nesting, etc.
- JSON(variable) returns a JSON string representing the contents of variable.
- JData(text, verbose) returns a Miva variable whose contents are built from the JSON data in text. If the verbose parameter is anything other than zero or null, the JSON parser will display some extra information for debugging purposes.
View or download the source code here.
A few implementation notes:
- In Miva Script, an empty variable (null string) is often equivalent to one that's undefined. So null elements will disappear from a structure when it is converted from JSON to Miva. An empty array or structure will be converted to a null string.
- If you try the sample JSON message (above), you'll note that the three named elements are in reverse alphabetical order, from "gamma" to "alpha." In the Miva data and regenerated JSON, they are rearranged into alphabetical order. This is Miva's standard behavior. When it serializes a variable, it sorts named members alphabetically with respect to their siblings. Since the elements are named, instead of numbered, this won't be a problem for most applications; the order really doesn't matter. Array elements are not rearranged: referring again to the sample, the array elements "red," "green," and "blue" stay in their original order.
- JSON doesn't support sparse arrays. When you convert a Miva array to JSON, the original array subscripts are lost. When you convert JSON to an array, the elements are numbered consecutively, starting with 1.
- The Miva-to-JSON function calls itself recursively to parse nested structures. So there may be a speed problem with large, deeply-nested structures. In general, these functions are intended for handling relatively small messages, not massive data transfers.
- The Jdata function can be useful in non-Json applications, as a handy way to build a structured variable with a single statement, instead of a series of MvASSIGNs. Here's an example that shows how to build a structure for a basket charge, which can then be passed to the library functon BasketCharge_Insert.
<MvASSIGN NAME="l.basket_charge" VALUE="{ JData('
{ "basket_id": ' $ g.Basket:basket_id $ ',
"module_id": ' $ l.module:id $ ',
"type": "SHIPPING",
"tax_exempt": 0,
"descrip": "ZipShip Express",
"amount": ' $ l.amount $ ',
"disp_amt": ' $ l.amount $ '
}', 0) }">
Or to quickly create an array of weekday names:
<MvASSIGN NAME="l.weekdays"
VALUE="{ JData('["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", Saturday"]', 0) }">
More information is available at JSON.org.
Please
with any bug reports, questions, etc. Thanks! Kent