Reading Json Data using Data-INTO

Discussions relating to the ScottKlement.com port of the open source YAJL JSON Reader/Generator. This includes the YAJL tool as well as the YAJLR4, YAJLGEN, YAJLINTO and YAJLDTAGEN add-ons from ScottKlement.com. http://www.scottklement.com/yajl/
Post Reply
kathan.p@gmail.com
Posts: 19
Joined: Wed Aug 04, 2021 7:27 pm

Reading Json Data using Data-INTO

Post by kathan.p@gmail.com »

Issue - I don't get any data in DATA DS other then "title" field (title field is getting populated correctly. Am i missing something??

JSON -

Code: Select all

{
  "data": {
    "instruction2": "FIRST SEAT",
    "seat1l3": "2356",
    "reset34": "Yes",
    "temp": "-78",
    "seat1l4": "326",
    "seat2l3": "3265",
    "instruction1": "** Daily Check Call **",
    "seat2l4": "986",
    "trl": "12345",
    "seat2a70": "253",
    "seat1a70": "895",
    "eta": "2021-08-18T11:23:24-07:00",
    "onsched": "Yes",
    "hub": "896586",
    "odo": "123658",
    "reasonoffsched": "traffic"
  },
  "title": "Daily Check Call - 01"
}
RPG Declaration -

Code: Select all

dcl-ds data qualified;
 instruction2 char(50);
 seat1l3     char(5);
 reset34     char(1);
 temp        char(3);
 seat1l4     char(5);
 seat2l3     char(5);
 instruction1 char(50);
 seat2l4     char(5);
 trl         char(6);
 seat2a70    char(5);
 seat1a70    char(5);
 eta         char(26);
 onsched     char(1);
 hub         char(7);
 odo         char(11);
reasonoffsched char(70);
title        char(50);
end-ds;
I am moving the data into a standalone character variable name JsonString (using it below in Data-INTO)

Statement -

Code: Select all

Data-into data %Data(JsonString:'doc=string allowextra=yes allowmissing=yes'') %PARSER('YAJLINTO');
.
Scott Klement
Site Admin
Posts: 666
Joined: Sun Jul 04, 2021 5:12 am

Re: Reading Json Data using Data-INTO

Post by Scott Klement »

The JSON has this structure:

Code: Select all

{
   data: {
      ..fields here...
   },
   title:
}
But the RPG has this structure:

Code: Select all

data : {
   ..fields here...
   title:
}
So they do not match. Every time you see a { in JSON, you need to have a DCL-DS. Every time you see a }, you need to have an END-DS.

I think you want your RPG to look like this:

Code: Select all

dcl-ds json qualified;
      
  dcl-ds data;
   instruction2 char(50);
   seat1l3     char(5);
   reset34     char(1);
   temp        char(3);
   seat1l4     char(5);
   seat2l3     char(5);
   instruction1 char(50);
   seat2l4     char(5);
   trl         char(6);
   seat2a70    char(5);
   seat1a70    char(5);
   eta         char(26);
   onsched     char(1);
   hub         char(7);
   odo         char(11);
   reasonoffsched char(70);
  end-ds;

  title        char(50);

end-ds;
And then:

Code: Select all

Data-into JSON %Data(JsonString:'doc=string case=convert') %PARSER('YAJLINTO');
I removed the allowextra=yes and allowmissing=yes -- I didn't understand why you had these, and they will certainly make problems harder to understand because RPG will ignore most errors with these set. I also added case=convert -- it's not really needed, here, since everything in the JSON is lowercase letters, but... for many json documents, it's needed, so I always include it.
kathan.p@gmail.com
Posts: 19
Joined: Wed Aug 04, 2021 7:27 pm

Re: Reading Json Data using Data-INTO

Post by kathan.p@gmail.com »

Thank you Scott. it's working now!

Little different question related to DS subfields:-

Code: Select all

"eta": "2021-08-18T11:23:24-07:00"
- is there a way i can break the this sub-field into yyyy, mm, dd ?
I can do it by moving this DS.eta into another DS and break it down but can i do it here only?

Code: Select all

dcl-ds json qualified;
      
  dcl-ds data;
   instruction2 char(50);
   seat1l3     char(5);
   reset34     char(1);
   temp        char(3);
   seat1l4     char(5);
   seat2l3     char(5);
   instruction1 char(50);
   seat2l4     char(5);
   trl         char(6);
   seat2a70    char(5);
   seat1a70    char(5);
   eta         char(26);                                                      
   onsched     char(1);
   hub         char(7);
   odo         char(11);
   reasonoffsched char(70);
  end-ds;

  title        char(50);

end-ds;
Scott Klement
Site Admin
Posts: 666
Joined: Sun Jul 04, 2021 5:12 am

Re: Reading Json Data using Data-INTO

Post by Scott Klement »

There's nothing built-in that will parse that timestamp... you'll have to write something of your own.
jonboy49
Posts: 207
Joined: Wed Jul 28, 2021 8:18 pm

Re: Reading Json Data using Data-INTO

Post by jonboy49 »

This is what I've been using with the Zoom APIs. Simple but it does the job.

Code: Select all

// DSs for re-mapping of Zoom timestamp to RPG compatible format       
//    Initial format is yyyy-mm-ddThh:mm:ssZ (e.g. 2020-05-08T19:41:44Z)
//    which appears to be ISO 8601                                     
                                                                    
Dcl-ds  zoomTimestamp  Qualified;                                      
   date    char(10);                                                   
   *n      char(1);                                                    
   hour    char(2);                                                    
   *n      char(1);                                                    
   minute  char(2);                                                    
   *n      char(1);                                                    
   second  char(2);                                                    
   *n      char(1);                                                    
End-Ds;                                                                
                                                                       
Dcl-ds  isoTimestamp  Qualified; 
   date    char(10);              
   *n      char(1)  inz('-');     
   hour    char(2);               
   *n      char(1)  inz('.');     
   minute  char(2);               
   *n      char(1)  inz('.');     
   second  char(2);               
End-Ds;  

eval-corr isoTimestamp = zoomTimestamp;           

This works for me because Zoom keeps to this format. Your mileage may vary. Having got in as an ISO timestamp I can use it any which way I want with RPG's date/time operations.
Post Reply