-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjsontime.rpgle
84 lines (72 loc) · 2.86 KB
/
jsontime.rpgle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
**free
//------------------------------------------------------------------
// Desc: This is an example of running curl and processing results in RPG as JSON
//
// Steps in process:
// Run QSHCURL curl command to get time from: date.jsontest.com as JSON.
// QSHCURL writes results to IFS file so they can be consumed.
// DATA-INTO reads the json results and parses with YAJLINFO parser.
//
// Requirements:
// YAJL library needed from ScottKlement.com and must be in library list
// https://www.scottklement.com/yajl
//
// QShell on i library QSHONI needs to be installed.
// https://www.github/com/richardschoen/qshoni
//
// Article ref links:
// https://www.itjungle.com/2018/01/22/guru-using-curl-interact-web-services/
//------------------------------------------------------------------
// Data structure for date.jsontest.com JSON results
dcl-ds results Qualified;
date varchar(15);
milliseconds_since_epoch packed(15);
time varchar(15);
end-ds;
// Display message
dcl-s msg char(52);
dcl-S cmderror int(10);
dcl-S cmdline char(4096);
dcl-S errMsg varchar(500);
dcl-S ifsoutput varchar(255);
// Command line execution
dcl-pr qcmdexc ExtPgm;
cmd char(4096) Const Options(*VarSize);
cmdLength packed(15: 5) Const;
end-pr;
// Set the IFS output file name for JSON
ifsoutput = '/tmp/jsontime.json';
// Build QSHCURL curl command to run to get date from date.jsontest.com
// STDOUT JSON results from curl go to IFS file /tmp/jsontime.json
cmdline='QSHONI/QSHCURL CMDLINE(''--url date.jsontest.com'') ' +
'IFSSTDOUT(*YES) ' +
'IFSFILE(''' + %trim(ifsoutput) + ''') ' +
'IFSOPT(*REPLACE)';
// Run QSHCURL command and handle any errors nicely.
monitor;
qcmdexc(%trim(cmdline):%len(%trim(cmdline)));
cmdError=0;
on-error;
cmdError=1;
endmon;
// If errors, exit the program
if cmdError = 1;
*inlr=*on;
return;
endif;
// Read JSON results with DATA-INTO and YAJLINTO
// as the parser.
data-into results
%data(%trim(ifsoutput)
: 'doc=file case=any')
%parser('YAJLINTO');
// Display parsed JSON result fields
msg='Date: ' + results.date;
dsply msg;
msg='Time: ' + results.time;
dsply msg;
msg= 'Milliseconds since epoch: ' +
%char(results.milliseconds_since_epoch);
dsply msg;
*inlr=*on;
return;