Products Resources Support About Us

Best method to convert between BASIC Dynamic Array and JSON


#1

I wondering if anyone has experience in converting a Dynamic Array to, and from, a simple JSON array.

The UDOxxx functions just do not make such a simple activity easy so I am in the process of writing some examples to see how if it is viable or if I should just use plain BASIC logic.

I’d live to find out if anyone else has been down this path and what lessons they learned from it.


#2

If you are only looking to get a JSON array, not an object, you could loop through the dynamic array and build the JSON array. Or what I like is using python.

i.e. ( Note simple code, recommend you checking type and raising exceptions )
mv_to_json.py
import u2py

def make_array( in_dyne ):
out = str(u2py.DynArray( in_dyne ).to_list())
return str(out)

Then do something like the following in BASIC
MV_TO_JSON
PRINT “enter file name”:;INPUT FILENAME
OPEN FILENAME TO FILE ELSE STOP “CAN NOT OPEN FILE”
PRINT “enter item id”:;INPUT ID
READ REC FROM FILE,ID ELSE STOP “CAN NOT READ THE ITEM”
json = PyCallFunction(“mv_to_json”, “make_array”, REC )
PRINT json

( Note the cad above is very simple and could also use a bit more error checking, but I wanted to get you the example. )

Lastly if your solution is using web services then look at return returning the parameters as JSON and not have the above to the marshaling )

Mike


#3

Thanks for working up the code. It is an interesting approach, but one that does not work for us.

I asked the question because I have found that sending Dynamic Array from BASIC to python via the PyCallFunction() call destroys the system delimiters (converting all delimiters to \0xfffd). We do run UV with NLS so I’m not sure if this plays a part in our experience.

I have since created the BASIC code to step through the Dynamic Array (using the REMOVE function) and manually build the JSON string. I have also coded the parsing of the JSON string back into a Dynamic Array using the UDOArray functions.

This seems to work but feels a bit clunky.

I am a bit surprised that the UDO functions do not easily facilitate the conversion of a Dynamic Array to/from JSON Arrays.


#4

I have used the following to create json arrays/files in a basic program

$INCLUDE UNIVERSE.INCLUDE UDO.H
SENTENCE = ‘SORT TRANSACTION.FILE CUSTNO LOCNO DIVISION’
SENTENCE := ’ INVID1 INVID2 INVID3 TYPE CREATED DUE’
SENTENCE := ’ AMOUNT BALANCE CURRENCY JOB PO NOTE’
SENTENCE := ’ ID.SUP’
SENTENCE := ’ TOXML ELEMENTS TO “’ : TEMP.DOC.ID:’”’
CRT TM:SENTENCE
EXECUTE SENTENCE
READ XMLDOC FROM XML.FILE, TEMP.DOC.ID:".xml" ELSE
ERROR.MSG = “Did not create the document (”:TEMP.DOC.ID:".xml)"
CRT ERROR.MSG
STOP
END
XMLDOC = CHANGE(XMLDOC, @FM, CHAR(13):CHAR(10))

  • LOAD XML INTO UDO TREE
    RESULT = UDORead(XMLDOC, UDOFORMAT_XML, hDoc)
    IF NOT(RESULT= UDO_SUCCESS) Then
    ERROR.MSG = “Did not create the document (”:TEMP.DOC.ID:".xml)"
    STATUS = UDOGetLastError(errcode, errmsg)
    CRT "Error “:errcode:” - ":errmsg
    ERROR.MSG<-1> = "Error “:errcode:” - ":errmsg
    STOP
    END
  • WRITE IT AS OUT AS JSON
    RESULT = UDOWrite(hDoc, UDOFORMAT_JSON, JSONDOC)
  • SAVE JSON STRING
    WRITESEQ JSONDOC TO OUTFILE ELSE STOP