Parsing errors with complex JSON

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
dr21958
Posts: 2
Joined: Fri Jun 07, 2024 3:02 pm

Parsing errors with complex JSON

Post by dr21958 »

I'm having some issues with what appears to be data-into parsing errors. These RPGLE's are right out of YAJLGEN, trying to read the same json files that generated the yajlgen code. IE, step 1: yajlgen, step 2, compile, Step 3-run....

I'm getting errors like:

call drtemp/json2byajl
The document for the DATA-INTO operation does not match the RPG variable;
reason code 7.
The call to READTHEJSO ended in error (C G D F).
F
The call to READTHEJSO ended in error (C G D F).
F
Application error. RNX0356 unmonitored by JSON2BYAJL at statement
0000000596, instruction X'0000'.

---

Message ID . . . . . . : RNX0356 Severity . . . . . . . : 50
Message type . . . . . : Escape
Date sent . . . . . . : 05/17/24 Time sent . . . . . . : 16:10:38

Message . . . . : The document for the DATA-INTO operation does not match
the RPG variable; reason code 7.
Cause . . . . . : While parsing a document for the DATA-INTO operation, the
parser found that the document does not correspond to RPG variable "jsondoc"
and the options do not allow for this. The reason code is 7. The exact
subfield for which the error was detected is
"jsondoc.ungrouped_customers(1)". The options are "doc=file case=convert
countprefix=num_". The document name is /home/DRIMA/CYB_JVA8/JSON2.JSON; *N
indicates that the document is not an external file. The parser is
'YAJLINTO'. *N indicates that the parser is a procedure pointer.
Recovery . . . : Contact the person responsible for program maintenance to
determine the cause of the problem.

Technical description . . . . . . . . : Reason codes and their meanings are
as follows:
1. The specified path to the name was not found in the document.
2. The document contains too few array elements for array subfields of a
data structure.
3. The document contains too many array elements for array subfields of a
data structure.
4. The document is missing information to match subfields.
5. The document contains extra names that do not match subfields.
6. The document contains text content within the content for the subfields
of a data structure.
7. The document contains subfield items for RPG scalar fields, subfields or
array elements.

JSON data files and yajlgen RPG's attached. Thanks :)
Attachments
Samples.zip
(12.82 KiB) Downloaded 75 times
Scott Klement
Site Admin
Posts: 736
Joined: Sun Jul 04, 2021 5:12 am

Re: Parsing errors with complex JSON

Post by Scott Klement »

YAJLGEN isn't intelligent... it makes a "best guess" at what the RPG definitions should be based on the document. That is why it inserts this comment:

Code: Select all

       // FIXME:
       //   - The field lengths (varchar/packed) are guesses
       //       and should be adjusted based on your business rules.
       //   - The array lengths (dim keywords) are also guesses
       //       and should be adjusted based on your business rules
It's trying to tell you something: You have to use a little bit of human intelligence.

For example, if the JSON contains this:

Code: Select all

      "description": "",
It can tell that it is a character field because of the "". But, how should it be defined in RPG? CHAR(10)? VARCHAR(100000)? How could it know? This is true throughout your documents, there are a lot of fields like this... so YAJLGEN has given you a start, but you need to go through and fill in the precise details. This is the nature of RPG -- you have to know all of the variable definitions at compile time.

None of the errors are parsing errors. They all have to do with RPG mapping the values into the data structure. That's why you are getting RPG errors (like the one you posted) rather than YAJL errors.

I loaded and tried your examples. Here is what I found:
  1. JSON2 ran without errors/problems for me. The error you posted sounds like a problem in RPG from the very early days of DATA-INTO... please be sure you are up-to-date on PTFs (this was a VERY long time ago... more than 5 years). No errors on my machine.
  2. JSON3 you have the correct DATA-INTO statement in your program, but it is commented-out. The one you have uncommented (beneath it) is missing the %PARSER option for fields that begin with a number. So when you get to 15_DAY_BALANCE it fails because RPG variables can't begin with a number like that. This is why YAJLGEN puts the option "number_prefix": "YAJL_" into the program, so it will become YAJL_15_DAY_BALANCE instead... but you commented this out.
  3. JSON4 Your documents[1].pages[] array has more than one array element, but the RPG definition says DIM(1). This is because documents[0].pages[] had only one element, so YAJLGEN put dim(1) on it... it only looks at the first array element when writing the data structure for you. Again... that is why it puts a comments saying that the variable lengths and array dimensions are GUESSES, and you need to apply human intelligence.
  4. JSON4 similar problem with the "documents[xxx].approvals" array. The first element has "approvals": []. So that shows an array with nothing at all in it, it doesn't know whether its a string, numeric, DS, or anything else because it bases the definition on the first element of documents[]. Since later elements do have data inside of them, RPG can't map the data later. Same problem... someone with human intelligence who knows the business rules, knows what is required for this document needs to go through and adjust things. YAJLGEN is just a tool to make things easier for you, it is NOT a substitute for the intelligence of a human.
dr21958
Posts: 2
Joined: Fri Jun 07, 2024 3:02 pm

Re: Parsing errors with complex JSON

Post by dr21958 »

Scott,
Thanks for the heads up and pointers.
Post Reply