An RPGers First Steps with JSON: Consuming JSON data with YAJL


There are two basic approaches we could take. The first is simply to test the node returned by YAJL_OBJECT_FIND to see if it is null. Remember that underneath it all the node values are pointers so the actual test could look like this:

      idNode = YAJL_OBJECT_FIND( customerNode: 'ID' );
         If idNode <> *null;
            customer(c).id = YAJL_GET_NUMBER( idNode );
            // Take appropriate error action

If ID were a compulsory field, which given its name is likely, then this is probably an appropriate way to handle it. But what if there are optional fields involved in the document? Is there an easier way than coding a test for each and every field?

The answer is yes. By using the YAJL_OBJECT_LOOP function we can simply loop through all of the objects within a given node, to obtain the field name for each. We can then use that name in a SELECT operation and extract the values as appropriate.

In the example below we have replaced the code (J, K, etc.) that extracted the fields within the YAJL_ARRAY_LOOP (I) with the following logic:

(M)       i = 0;

(N)       Dow YAJL_OBJECT_LOOP( customerNode: i: key: node );
(O)          When key = 'ID';
                customer(c).id = YAJL_GET_NUMBER( node );
             When key = 'Name';
                customer(c).name = YAJL_GET_STRING( node );
             When key = 'Street';
                customer(c).street = YAJL_GET_STRING( node );
             When key = 'City';
                customer(c).city = YAJL_GET_STRING( node );
             When key = 'State';
                customer(c).state = YAJL_GET_STRING( node );
             When key = 'Zip';
                customer(c).zip = YAJL_GET_STRING( node );

We begin by setting the item counter (i) to zero. It will subsequently be incremented by the YAJL_OBJECT_LOOP function at (N). This function requires four parameters. The first is the node locator from which we are to extract the objects—c customerNode in our case. The second is the aforementioned item counter. The third is a character field into which the function will place the key value (i.e. the object's name) and it will place the actual locator node in the fourth parameter. Like YAJL_ARRAY_LOOP this function returns an indicator that is used to control the DOW loop.

We can then code a SELECT and test for the individual field names in the WHEN clauses that begin at (O). In practice, when using this approach, we would almost certainly need to add logic to ensure that compulsory items were in fact present. We would also need to ensure that appropriate default values were set for the optional fields. For example by ensuring that the optional character fields in the array DS were blank.

Wrapping Up

There are a lot more features and functions available within YAJL than we have had time to cover in this brief two part series, but we hope we have whetted your appetite and given you a good starting point for further explorations.

We hope to return to YAJL in future articles. In the meantime, if there are any specific problems or issues that you would like us to address, please let us know via the comments section.

Jon Paris is a technical editor with IBM Systems Magazine and co-owner of Partner400.

Susan Gantner is a technical editor with IBM Systems Magazine and co-owner of Partner400.

Like what you just read? To receive technical tips and articles directly in your inbox twice per month, sign up for the EXTRA e-newsletter here.

comments powered by Disqus



2018 Solutions Edition

A Comprehensive Online Buyer's Guide to Solutions, Services and Education.

New and Improved XML-INTO

Namespace support makes the opcode a viable option

Authenticating on the Web

The finer points of OpenRPGUI, Part 1

The Microphone is Open

Add your voice: Should IBM i include open-source RPG tools?

IBM Systems Magazine Subscribe Box Read Now Link Subscribe Now Link iPad App Google Play Store
IBMi News Sign Up Today! Past News Letters