• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: Parsing JSON With DATA-INTO And A Twist

    September 23, 2019 Mike Larsen

    One of my recent projects required me to parse JSON returned from a web service. On the surface this sounded like a pretty easy task, but I quickly ran into a challenge. The JSON being retuned didn’t have a top-level element, and since I wanted to load the JSON into a data structure, my program couldn’t handle it.

    After some searching, I found that some of my options included changing the code in the parser (JSONPARSE) or using a totally different parser altogether. While both of these are viable options, I decided to take a slightly different route.

    Before I explain the solution, it makes sense to show what the JSON looks like (Figure 1).

    This story contains code, which you can download here.

    Figure 1. JSON to be parsed.

    When using DATA-INTO with a data structure, all elements of the data structure must match exactly to the JSON being loaded into it. When I create a data structure, I need to name it and each variable within. But, as you can see, there isn’t a top-level element in my JSON. Therein lies the problem; I have to name the top-level of my data structure and it needs to match the top-level of the JSON. So, when I created a data structure that I wanted to hold the JSON (Figure 2), DATA-INTO fails because the variables in the data structure don’t exactly match the JSON object (it fails at the top-level ‘itemDs’).

    The following code shows the data structure to contain JSON.

    dcl-ds itemDs        Qualified;
           success       char(5);
           classdesc     char(30);
           numberofitems char(5);
    
       dcl-ds itemlist      dim(50) inz;
              itemnumber    char(5);
              description   char(30);
              unitofmeasure char(5);
              itemclass     char(5);
    
       end-ds itemlist;
    
    end-ds itemDs;      
     
    

    So, without modifying the parser or using a different parser, how did I overcome this challenge? The answer is to add a top-level element to the JSON being returned from the web service. Ok, that sound great, but how do I do that?

    I’m using the SQL function HTTPGETCLOB to consume the web service, so all I have to do is wrap the JSON result within a JSON object. The JSON object will now be the highest level and I named it to match the top-level in my data structure (itemDs). This next piece of code shows HTTPGETCLOB with concatenated JSON object.

      // since the highest level of the json array isn't named, we need to
      // apply one to it so we can have it match the data structure we're
      // loading the json into. That is done by concatenating ' "itemDs" : '
      // with the HttpGetClob function.
    
      Exec Sql
        Select '{ "itemDs" : ' concat Systools.HttpGetClob
                (:WebServiceUrl , :WebServiceHeaderJson) concat '}'
         into :itemJson
         from sysibm.sysdummy1;
      
    

    With the JSON object in place, I can now parse it using DATA-INTO and the data structure I built.

    Data-into itemDs %Data(%trim(itemJson):
                  'case=any allowextra=yes')
                   %Parser('JSONTSTLIB/JSONPARSE');  
    

    Note that I’ve also used this technique in the past using the SQL function JSON_TABLE and it works the same.

    There are often many different solutions to problems. I hope this serves as another option if you’re faced with a similar situation. The complete code for consuming a web service and parsing the returned JSON is available for download.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: 400guru, FHG, Four Hundred Guru, IBM i, JSON

    Sponsored by
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    As I See It: Restless Third Party Maintenance An Option As Power7 Goes EOL

    3 thoughts on “Guru: Parsing JSON With DATA-INTO And A Twist”

    • Jon Paris says:
      September 23, 2019 at 11:02 am

      While the proposed solution will work well in this set of circumstances it still has what for me remains a major problem. Namely that the JSON parser supplied by IBM was never intended for production work – it was intended to demonstrate how to write a parser. The issue encountered by the writer is a direct result of this not being “production ready” – Scott Klement’s YAJLINTO parser for example does not suffer this problem.

      Reply
    • John McKay Congerton says:
      September 29, 2019 at 6:23 am

      Do it in Python

      Reply
      • Jon Paris says:
        October 5, 2019 at 5:41 pm

        If the web service (or whatever) consuming the JSON is written in RPG why one earth would you want to jump through hoops trying to interface with Python for this? Python makes a lot of sense for many, many tasks but I think lately it has started to be proposed as the solution to everything. This has been happening with SQL for some time – I guess it is JSON’s turn.

        “The answer is Python! – Now what was your question?”

        Reply

    Leave a Reply Cancel reply

TFH Volume: 29 Issue: 53

This Issue Sponsored By

  • Fresche Solutions
  • ProData Computer Services
  • New Generation Software
  • Computer Keyes
  • WorksRight Software

Table of Contents

  • You’ve Still Got Friends In High Places
  • Third Party Maintenance An Option As Power7 Goes EOL
  • Guru: Parsing JSON With DATA-INTO And A Twist
  • As I See It: Restless
  • Relieving The Anxiety About IBM i Performance

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • To Comfort The Afflicted And Afflict The Comfortable
  • How FalconStor Is Reinventing Itself, And Why IBM Noticed
  • Guru: When Procedure Driven RPG Really Works
  • Vendors Fill In The Gaps With IBM’s New MFA Solution
  • IBM i PTF Guide, Volume 27, Number 27
  • With Power11, Power Systems “Go To Eleven”
  • With Subscription Price, IBM i P20 And P30 Tiers Get Bigger Bundles
  • Izzi Buys CNX, Eyes Valence Port To System Z
  • IBM i Shops “Attacking” Security Concerns, Study Shows
  • IBM i PTF Guide, Volume 27, Number 26

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle