|
So if you check rates, is it NULL?
I know some still like to parse this way, but personally I prefer using
DATA-INTO with YAJLINTO parser. Basically one line of code to parse the
entire JSON string into a DS.
Scott even created a utility to create the DS for you.
-----Original Message-----
From: RPG400-L [mailto:rpg400-l-bounces@xxxxxxxxxxxxxxxxxx] On Behalf Of
Robert Rogerson
Sent: Wednesday, February 05, 2020 9:27 PM
To: RPG programming on the IBM i (AS/400 and iSeries) <
rpg400-l@xxxxxxxxxxxxxxxxxx>
Subject: Parsing json with YAJL
Hi All,
I'm trying to parse a simple json document using yajl. Here is the code I
have.
dcl-proc GetCurrency ;
dcl-pi *n char(52);
pJson char(32766);
end-pi;
dcl-s i int(10);
dcl-s j int(10);
dcl-s docNode like(yajl_val);
dcl-s node like(yajl_val);
dcl-s val like(yajl_val);
dcl-s rates like(yajl_val);
dcl-s errMsg varChar(500);
dcl-s key varChar(50);
dcl-s retMsg like(msg);
// In debug pJson = {"success":true,
"timestamp":1580953386,
"base":"EUR",
"date": "2020-02-06",
"rates":{"USD":1.099578,"CAD":1.460943}}
docNode = yajl_string_load_tree(pJson :errMsg);
If errMsg <> *blanks;
Return *blanks;
Endif;
node = yajl_object_find(docNode :'base');
errMsg = yajl_get_string(node);
// In debug errMsg = 'EUR'
rates = yajl_object_find(docNode :'rates');
i = 0;
Dow yajl_array_loop(rates :i :node);
// In debug this loop is never entered so yajl_array_loop is returning
false
j = 0;
Dow yajl_object_loop(node :j :key :val);
If key = 'USD';
retMsg = 'Rate for USD = ' + yajl_get_string(val);
Leave;
Endif;
If retMsg <> *blanks;
Leave;
Endif;
Enddo;
Enddo;
yajl_tree_free(docNode);
Return retMsg;
end-proc;
In debug I can see that I am able to find the node 'base' but I am unable
to find the node 'rates'.
Can anyone see what I'm missing?
Thanks,
Rob
--
This is the RPG programming on IBM i (RPG400-L) mailing list
To post a message email: RPG400-L@xxxxxxxxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: https://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxxxxxxxx
Before posting, please take a moment to review the archives
at https://archive.midrange.com/rpg400-l.
Please contact support@xxxxxxxxxxxx for any subscription related
questions.
Help support midrange.com by shopping at amazon.com with our affiliate
link: https://amazon.midrange.com
--
This is the RPG programming on IBM i (RPG400-L) mailing list
To post a message email: RPG400-L@xxxxxxxxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: https://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxxxxxxxx
Before posting, please take a moment to review the archives
at https://archive.midrange.com/rpg400-l.
Please contact support@xxxxxxxxxxxx for any subscription related
questions.
Help support midrange.com by shopping at amazon.com with our affiliate
link: https://amazon.midrange.com
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.