Working with JSON in RPG
1. What is JSON?
JSON is a format for encapsulating data as it's sent over networks. JSON is self-describing (field names are in the data itself) and human-readable. (Much Like XML).
Very popular in Web Services and AJAX.
Can be used by all major programming languages.
JSON is simpler:
- only supports UTF-8, whereas XML supports a variety of encodings.
- doesn't support schemas, transformations.
- doesn't support namespaces
- method of "escaping" data is much simpler. JSON is faster
- more terse (less verbose). About 70% of XML's size on average
- simpler means faster to parse
- dead simple to use in JavaScript
2. The YAJL JSON reader/writer
YAJL = Yet Another JSON Library
- Created by Lloyd Hilaiel (who works for Mozilla)
- completely Open Source (very permissive ISC license)
- Extremely fast. (Fastest one we benchmarked)
- Written in C.
- Bindings available for Ruby, Python, Perl, Lua, Node.js and others
3. Generating JSON in RPG Code • Example
4. Reading JSON in RPG Code • Example with DATA-INTO • Example with YAJL subprocedures
YAJL provides sets of routines for:
- Generating JSON data
- Parsing JSON data in an event-driven (SAX-like) manner
- Parsing JSON in a tree (DOM-like) manner I have found the tree-style routines to be easier to work with, so will use them in my examples. Scott's RPG adapter additionally provides
- YAJLINTO – a DATA-INTO interface for reading JSON
- YAJLDTAGEN – a DATA-GEN generator for creating JSON