Parsing JSON in G2

Overview

This article provides information about a procedure in G2 that can be used to parse JSON data.

Information

G2 2020.4 and up provides the g2-unmarshal-json procedure, which parses JSON text into G2 sequences and  structures. This is the only current process to parse JSON data inside of G2. For marshalling, regular G2 interpolating is recommended. 

The g2-unmarshal-json procedure can be found in the g2web module. To be able to use it, this module needs to be merged into the current KB. The G2web module can be found in the installation directory under "<installation-dir>\g2\kbs\utils\g2web.kb". 

This procedure follows the same restrictions as the XML parse procedures. Because of this, it is not recommended to parse large JSON files. While reading or transferring very large strings, G2 performs no other processing.

The attached KB illustrates how the procedure can be used to parse a small JSON. To see it in action, load the KB and start G2. Once the KB is started, press the Start Unmarshal button. The procedure will post to the message board the JSON parsed into a structure object.

Note: G2 versions 2020 need a license which is different from the previous versions. If you need to upgrade, contact support and request the appropriate license for your servers.

Attachments

Comments

1 comment

  • Avatar
    Jean-Marie Foret

    A few comments:

    • It would be interesting to know the limitation factors for applying this procedure. Is it because of the size of the text (G2 Reference Manual says the max is 1,000,000 characters) or the uninterrupted execution time? If the time is the limiting factor, then it'd be nice to have a mechanism to control that or at least to know if this procedure has its own uninterrupted-procedure-execution-limit or whether it uses the default one.
    • JSON syntax very often uses camelCase convention. Could we have an option to support that? For example if I would call this procedure with "{"camelCaseSyntax": 123}", it would return a structure like: structure (CAMEL-CASE-SYNTAX: 123), instead of structure (CAMELCASESYNTAX: 123). I have written my own version of a JSON parser and I've included this option. I would love to use g2-unmarshal-json , but without this option I can't.
    • Maybe worth mentioning that the parsing supports true/false and null, i.e. when parsing: {"t": true, "f": false, "n": null} it returns: structure (T: true, F: false, N: the symbol NULL) AND does not like embedded " inside a text, i.e. parsing: {"t": "My name is @"John@""}, generates an error.
    • Please include this function in a utilities/g2web.pdf file ;-) 
    0
    Comment actions Permalink

Please sign in to leave a comment.