• 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
    Rocket Software

    Unlock the full potential of your data with Rocket Software. Our scalable solutions deliver AI-driven insights, seamless integration, and advanced compliance tools to transform your business. Discover how you can simplify data management, boost efficiency, and drive informed decisions.

    Learn more today.

    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

  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24
  • Big Blue Raises IBM i License Transfer Fees, Other Prices
  • Keep The IBM i Youth Movement Going With More Training, Better Tools
  • Remain Begins Migrating DevOps Tools To VS Code
  • IBM Readies LTO-10 Tape Drives And Libraries
  • IBM i PTF Guide, Volume 27, Number 23

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