Cannot read JSON file with CCSID 37
Posted: Thu Nov 21, 2019 1:28 am
I am parsing a JSON file (screens.jsn) created by Axes but getting an error "The JSON root element must be either an array or an object (offset = 0)". This is how my code looks like:
I get the error when it set the SourceFile (#JSONReader.SetSourceFile). Looking at the file from iSeries, the CCSID is set to 37 and it seems like it is not reading the file properly. I change the CCSID to 819 but the array symbol '[' is translated into something else causing the same error.
I have a work around that worked. I leave the CCSID to 819. FTP the file to my local PC as ASCII, then ftp back to the iSeries folder which is now CCSID 819. It worked and I didnt encounter any error. Since this work around won't work for a client, my question is, how will you read a JSON file with CCSID 37? Or how can I convert the file so that Json Reader can read it?
This is what the file looks like with CCSID 37. #XPRIM_JsonReader cannot read it.

When you convert it to CCSIS 819 (using CHGATR command), this what it looks like. You'll see the first character is translated.

Code: Select all
Define_Com Class(#XPRIM_JsonReader) Name(#JSONReader) Reference(*DYNAMIC)
Define_Com Class(#XPRIM_ErrorInfo) Name(#ErrorObject)
Define_Com Class(#XPRIM_JsonObject) Name(#JsonObj) Reference(*DYNAMIC)
Define_Com Class(#XPRIM_File) Name(#JsonFile)
Define_Com Class(#PRIM_BOLN) Name(#wk_found)
Define Field(#ErrMsg) Type(*CHAR) Length(200)
#ErrMsg := *Blank
#JsonFile := #STD_QSEL.Trim
#JSONReader <= *New #XPRIM_JsonReader
#JSONReader.SetSourceFile Path(#JsonFile.path) Errorinfo(#ErrorObject)
* if the json is not properly formed
If (*Not #errorobject.OK)
#ErrMsg := #ErrorObject.ErrorMessage.AsNativeString
Message Msgtxt(#ErrMsg)
Return
Endif
* start processing the file
#JSONReader.ReadBeginArray Found(#wk_found)
* check if orders were returned
If (#wk_found)
* loop over all orders
#JSONReader.MoveNext Ok(#wk_found)
Dowhile (#wk_found *And (*Not #JSONReader.IsEndArray))
#JSONReader.ReadObject Found(#wk_found) Result(#JsonObj)
#JSONReader.MoveNext Ok(#wk_found)
*** Parse the data from JsonObj here ****
Endwhile
Endif
I have a work around that worked. I leave the CCSID to 819. FTP the file to my local PC as ASCII, then ftp back to the iSeries folder which is now CCSID 819. It worked and I didnt encounter any error. Since this work around won't work for a client, my question is, how will you read a JSON file with CCSID 37? Or how can I convert the file so that Json Reader can read it?
This is what the file looks like with CCSID 37. #XPRIM_JsonReader cannot read it.

When you convert it to CCSIS 819 (using CHGATR command), this what it looks like. You'll see the first character is translated.
