Page 1 of 1

Cannot read JSON file with CCSID 37

Posted: Thu Nov 21, 2019 1:28 am
by IvanR
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:

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 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.
Image

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

Re: Cannot read JSON file with CCSID 37

Posted: Fri Nov 22, 2019 1:22 pm
by AbelS
According to https://docs.lansa.com/14/en/lansa015/i ... 0_0710.htm XPRIM_File expects the contents to be encoded in UTF-8.

Try creating a copy of the file in UTF-8:

In QShell
1. Change your directory to where the file is (alternatively provide full paths)

Code: Select all

cd <your directory>
2. Convert file and pipe it to your target file

Code: Select all

iconv -f37 -t1208 screens.jsn > screen1208.jsn
3. Set the CCSID of your target file

Code: Select all

setccsid 1208 screens1208.jsn
HTH
Abel