Lexical Error while reading JSON payload through webservice

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
pshankmd
Posts: 11
Joined: Tue May 02, 2023 2:14 am

Lexical Error while reading JSON payload through webservice

Post by pshankmd »

Hello,
Trying to read the JSON payload (sent through web service/API) in ILE RPG using below logic, but ends up in error while invoking it through PostMan:
Error message – “'json parse: lexical error: invalid char in json text”

Could you please help to understand what am I missing here?
Let me know if you need further information.

Thank you!

Input Payload:

Code: Select all

{
  "Header":[
                {
                                "TransactionID":"123456",
                                "AccountNumber":87654321                      
                }
   ],
  "Detail":[
                {
                  "TransactionType":"P",
                  "AccountType":"J",
          "Contact":[
                                {
                                  "ContactIndex":"DELIVERY",
                                  "ContactGroupNum":1,
                                },
                                {
                                  "ContactIndex":"DELIVERY",
                                  "ContactGroupNum":2,
                                }
                     ],
                  "ChangeUser":"UPDUSER"
                }
  ]
}

Code snippet to retrieve the payload:

Code: Select all

Dcl-Proc GetJSONDocument;
 
  Dcl-PI *N Ind;
    Customer LikeDS(customer_T);
    DocNode Like(yajl_val);
  End-PI;
 
  Dcl-DS ErrorCode;
    *n int(10) inz(0);
    *n int(10) inz(0);
  End-DS;
 
  Dcl-PR getenv pointer extproc(*dclcase);
      var pointer value options(*string);
  End-PR;
 
  Dcl-S contentLength Int(10);
  Dcl-S fd        Int(10);
  Dcl-S inpBuf    Pointer;
  Dcl-S inpLength Int(10);
  Dcl-S outBuf    Char(65535) Based(p);
  Dcl-S p         Pointer;
 
  Dcl-C O_WRONLY 2;
  Dcl-C O_TEXTDATA 16777216;
 
  DocNode = yajl_stdin_load_tree(*On: ErrMsg);
  
  If ErrMsg <> '';
    Customer.HttpStatus = 500;
    Customer.ErrorMsg = 'json parse: ' + errMsg;
    Customer.Success = *off;
  EndIf;
Declarations:

Code: Select all

Dcl-DS header_t qualified template;
   TransactionID  char(50)    inz(' ');
   AccountNumber  zoned(8)    inz;
End-DS;
 Dcl-DS contact_t qualified template;
   ContactIndex        char(15)    inz('');
   ContactGroupNum     zoned(5)    inz;
End-DS;
 Dcl-DS detail_t qualified template;
   TransactionType        char(1)    inz(' ');
   AccountType            char(1)    inz(' ');
   Contact         likeds(Contact_t) dim(999) inz(*likeds);
   ChangeUser             char(10)   inz(' ');
End-DS;
 Dcl-DS customer_t qualified template;
   success    ind                inz(*on);
   httpStatus packed(3)          inz(0);
   errorMsg   varchar(500)       inz('');
   header     likeds(header_t)   inz(*likeds);
   detail     likeds(detail_t)   inz(*likeds);
End-DS;
Dcl-DS Customer likeds(Customer_t) inz(*likeds);
Scott Klement
Site Admin
Posts: 658
Joined: Sun Jul 04, 2021 5:12 am

Re: Lexical Error while reading JSON payload through webservice

Post by Scott Klement »

In the JSON document, I see two errors:

Code: Select all

		"Contact": [{
				"ContactIndex": "DELIVERY",
				"ContactGroupNum": 1,      <-- invalid comma
			},
			{
				"ContactIndex": "DELIVERY",
				"ContactGroupNum": 2,     <-- invalid comma
			}
		],
When you separate things with commas, they must be in between fields, but not after the last field in an object or array, You can test the document yourself by pasting it into something like jsonlint at https://jsonlint.com/

That said, because you are showing us what the JSON "looks like" instead of sending the actual file, I also cannot be sure that there aren't any invalid characters that are unprintable -- and therefore don't display in your message. So I would recommend copy/pasting the exact file from your system (open it with someting like Notepad, Notepad++, VS Code or RDi, and copy/paste it into jsonlint)

This should help you indentify any problems with the document.

Also most of your definitions in your code are unrelated to your question. You show a bunch of data structure and constant definitions that aren't used at all by your code sample. Can you explain why you included those? Are you having problems with them?
pshankmd
Posts: 11
Joined: Tue May 02, 2023 2:14 am

Re: Lexical Error while reading JSON payload through webservice

Post by pshankmd »

Hello Scott,
Never mind, the actual JSON has more fields, when truncated to paste here, forgot to remove the comma. Already checked with json formatter to confirm the payload and did not get any errors.

Again did copy/paste the whole JSON file content into jsonlint and when validated, got the result as 'valid JSON'. So dont find any problems with the document.

I have included the declarations because, customer_t is referred in GetJSONDocument procedure, and that customer DS is the one that gets data from DocNode later in the process. You can please ignore it.

The statement that is failing: DocNode = yajl_stdin_load_tree(*On: ErrMsg)
Error message receiving- lexical error: invalid char in json text

Is it possible to check what is the exact error/invalid char in json text?
Do you have any suggestions on how to fix it? Or any alternative approaches I can follow?

Thank you!
pshankmd
Posts: 11
Joined: Tue May 02, 2023 2:14 am

Re: Lexical Error while reading JSON payload through webservice

Post by pshankmd »

Hello Scott,
Found what got missed. When testing through PostMan, believe it did not like manually added CRLF (from notepad sample). Removed all line breaks and made it as a single continuous string and submitted as JSON, and now no error message :)

Thank you!
Post Reply