Cannot read JSON file with CCSID 37

This Q&A forum allows users to post and respond to "How Do I Do ....." questions. Please do not use to report (suspected) errors - you must use your regional help desk for this. The information contained in this forum has not been validated by LANSA and, as such, LANSA cannot guarantee the accuracy of the information.
Post Reply
Posts: 6
Joined: Fri Nov 10, 2017 6:39 am

Cannot read JSON file with CCSID 37

Post by IvanR » 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:

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)

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

Posts: 33
Joined: Wed Nov 16, 2016 1:43 pm
Location: Sydney, Australia

Re: Cannot read JSON file with CCSID 37

Post by AbelS » Fri Nov 22, 2019 1:22 pm

According to ... 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

Post Reply