× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



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

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

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.