• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: Parsing JSON That Has Spaces In The Key

    July 8, 2024 Mike Larsen

    One of my favorite tasks is working with APIs. I’ve worked with many APIs over the years, and some introduce unique challenges that must be overcome. Recently, I worked on a project where I was to consume a REST API that provided a JSON payload. While that seems straight forward, I quickly discovered a challenge.

    The JSON payload had keys that have spaces in them (Figure 1). That may be considered bad practice, but I still needed to figure out how I could parse it correctly. Having never seen JSON like this before, I started with a Google search. I found some examples that were similar, but they were on different platforms and not something that was useful in my case. The JSON payload is shown below and I’ve changed the data elements so I don’t expose proprietary information.

    Figure 1. JSON payload

    {
    	"data": [{
    			"FirstName": "John",
    			"LastName": "Doe",
    			"City": "Paramus",
    			"State": "NJ",
    			"Zip code": "07082",
    			"Date of birth": "11/12/1952",
    			"Favorite color": "Blue"
    		},
    		{
    			"FirstName": "Jane",
    			"LastName": "Doe",
    			"City": "Milford",
    			"State": "PA",
    			"Zip code": "08596",
    			"Date of birth": "02/23/1945",
    			"Favorite color": "Red"
    		},
    		{
    			"FirstName": "Harry",
    			"LastName": "Henderson",
    			"City": "Palm Springs",
    			"State": "CA",
    			"Zip code": "90258",
    			"Date of birth": "05/25/1925",
    			"Favorite color": ""
    		},
    		{
    			"FirstName": "Juli",
    			"LastName": "Ambrose",
    			"City": "Austin",
    			"State": "TX",
    			"Zip code": "85247",
    			"Date of birth": "09/01/1985",
    			"Favorite color": ""
    		},
    		{
    			"FirstName": "Ron",
    			"LastName": "Nettles",
    			"City": "Virginia Beach",
    			"State": "VA",
    			"Zip code": "09874",
    			"Date of birth": "07/30/1971",
    			"Favorite color": "Orange"
    		},
    		{
    			"FirstName": "Betty",
    			"LastName": "Winn",
    			"City": "Rochester Hills",
    			"State": "MI",
    			"Zip code": "02547",
    			"Date of birth": "11/01/1990",
    			"Favorite color": "Brown"
    		}
    	]
    }
    

    Looking at the JSON payload, there are three keys that have embedded spaces (Zip code, Date of birth, and Favorite color). I use the SQL function, Json_table, to parse JSON payloads. Needing to figure this out on my own, I started trying different variations of code. After a few attempts, I found something that worked. The final code is shown below (Figure 2).

    Figure 2. Json_table SQL code

    Select
       Coalesce(x.FirstName, ' ')     as FirstName,
       Coalesce(x.LastName, ' ')      as LastName,
       Coalesce(x.City, ' ')          as City,
       Coalesce(x.State, ' ')         as State,
       Coalesce(x.ZipCode, ' ')       as ZipCode,
       Coalesce(x.DateOfBirth, ' ')   as DateOfBirth,
       Coalesce(x.FavoriteColor, ' ') as FavoriteColor
    
    from Json_table
    ( :variableWithJsonPayload
    ,
           '$'
    
             Columns                                  
                (Nested '$.data[*]'                            
                  Columns                                      
                    (FirstName      varchar(25) Path '$.FirstName',
                     LastName       varchar(25) Path '$.LastName',
                     City           varchar(25) Path '$.City',
                     State          varchar(2)  Path '$.State',
                     ZipCode        varchar(10) Path '$."Zip code"',
                     DateOfBirth    varchar(10) Path '$."Date of birth"',
                     FavoriteColor  varchar(15) Path '$."Favorite color"'
                    )           
                )
    ) as x;           
    

    Note that I have the JSON payload populated into variable variableWithJsonPayload as I’m using the SQL function in an RPG program. The solution to parsing JSON that has spaces in the keys is to put quotes around the string that is defined in the path (see columns Zip code, Date of birth, and Favorite color). When I did that, the entire string was considered versus just the word up to the space. When I run the SQL statement, I get the desired results (Figure 3).

    Figure 3. Result of parsed JSON payload

    I’ve added this SQL statement to my toolbox as I’m sure I will encounter this situation again. Hopefully this can help save you time if you face the same challenge.

    Mike Larsen is a director of information technology at Auburn Pharmaceutical and has been working with IBM i systems for over 20 years. He specializes in RPG, CL, and SQL and recently has been working with PHP and Python. Current projects have given Mike the opportunity to work with generating and parsing XML and JSON from SQL and consuming SOAP and REST web services. Although his main area of expertise is on IBM i, Mike has a passion for learning other languages and how he can integrate other platforms with IBM i.

    RELATED STORIES

    Guru: Partitioning Result Sets Using SQL

    Guru: Comparing IFS Directories Using SQL

    Guru: String Manipulation Using SQL

    Guru: Regular Expressions, Part 1

    Guru: Regular Expressions, Part 2

    Guru: Debugging SQL Stored Procedures With ACS

    Guru: Creating PDF Documents With Python

    Guru: Creating Excel Spreadsheets With Python

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: 400guru, API, FHG, Four Hundred Guru, IBM i, JSON, REST API, RPG, SQL

    Sponsored by
    OCEAN User Group

    OCEAN TechCon24
    Four Days of Inspiration & Innovation!

    July 24, 2024 – ONLINE KICKOFF

    July 25, 26, & 27, 2024 – Community Center at the Marketplace – Tustin, CA

    Let’s engage at TechCon24! We’re passionate about affordable IBM i education presented by world class speakers!

    TechCon24 includes an Opening Keynote by Liam Allan & Scott Forstie, AI Experts Panel Discussion, CIO Summit/Roundtable with Alan Seiden & Special Guests, a Premier Vendor Solutions Expo, and Saturday Workshops! After a full day of training on Thursday evening, join us at the Casino Night.

    Register Now!

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Xorux Is Gaining Traction With LPAR2RRD System Monitoring Thoroughly Modern: The Synon Transformation Journey, From Legacy To Modern Java Solutions

    Leave a Reply Cancel reply

TFH Volume: 34 Issue: 32

This Issue Sponsored By

  • ARCAD Software
  • Fresche Solutions
  • OCEAN User Group
  • Computer Keyes
  • Manta Technologies

Table of Contents

  • Big Blue Working On Replacement For Global Solutions Directory For IBM i
  • Thoroughly Modern: The Synon Transformation Journey, From Legacy To Modern Java Solutions
  • Guru: Parsing JSON That Has Spaces In The Key
  • Xorux Is Gaining Traction With LPAR2RRD System Monitoring
  • IBM i PTF Guide, Volume 26, Numbers 24 And 25

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