-2

I am trying to parse below JSON using YAJL. YAJLGEN generated below data structure but the issue i am facing is the number of arrays ex: KEY, CUSTOMER are not fixed. These arrays are returned for each field in the response. I am trying to avoid defining an array for each field from the response.

Could you please, advise if there is a better way to read the below json and parse dyanic arrays. I tried using "yajl_array_loop", "yajl_array_elem" but i couldn't able to make it work in my program for some reason. Thank is in advance.


{ "errstatus": 400, "errors": { "Key": [ "The Key field is required." ], "Customer": [ "The Customer field is required." ] } }

dcl-ds jsonDoc qualified;
errstatus packed(3) inz(0);
dcl-ds ERRORS;
num_KEY int(10) inz(0);
KEY varchar(37) inz('') dim(1);
num_CUSTOMER int(10) inz(0);
CUSTOMER varchar(43) inz('') dim(2);
end-ds;
end-ds;

Rob
  • 1
  • 2
    Hi Rob, I think it would be a good idea if you showed a bit more of your code where you are calling the yajl functions, and also explain a bit more about how they are not working. – Barbara Morris Apr 05 '21 at 10:45
  • Rob, my understanding is that the data structure created by YAJLGEN is intended to be used with the DATA-INTO opcode, where you would code %PARSER('YAJLINTO'). where you don't have to actually call any of the yajl_ functions. If you are processing the JSON directly by calling the yajl_ functions, you can define your RPG variables any way you like. You don't even need a data structure. – Barbara Morris Apr 05 '21 at 10:58
  • Agree with @BarbaraMorris , also YAJLGEN just give you a place to start. It's up to you to refine it's output to your actual needs. Thus the reason for the comment `// FIXME - The array lengths (dim keywords) are also guesses and should be adjusted based on your business rules` – Charles Apr 05 '21 at 14:58
  • It's also quite possible to do this dynamically using yajl_array_loop, yajl_array_elem, etc. If you clean up your question, fix the formatting, and post your source code that you wrote that wasn't working, we could probably point out the actual issue to move you along. Myself and probably others don't want to write up an entire example from scratch to answer your question because we have other things to be doing. The effort you get in answering your question is highly correlated to the effort you put into asking it. See also: https://stackoverflow.com/help/how-to-ask – Player1st Apr 15 '21 at 20:46

1 Answers1

-3

If yajl is not working then it is probably not a good choice for your case. If your JSON is not hundreds of megabyte big then you may try a DOM like approach like using noxDB (https://github.com/sitemule/noxDB). It reads the whole JSON into memory and you can evaluate the in-memory JSON the way you want. Seems like a much better approach for your situation.

Mihael
  • 255
  • 1
  • 5