Rocket U2 | UniVerse & UniData

 View Only
  • 1.  UniDynArray in Visual Studio C#

    Posted 08-01-2023 06:10

    Hi,

    I have Universe pgms that return a UniDynArray, but would like to convert the returns into a json format.
    I am not sure if this has been asked before but really looking for help on this last section of my code please?



    ------------------------------
    Daniel Conradie
    Rocket Forum Shared Account
    ------------------------------


  • 2.  RE: UniDynArray in Visual Studio C#

    Posted 08-02-2023 06:58
    Edited by John Jenkins 08-02-2023 07:01

    Daniel,

    For clarification, is this a UniObjects client-side question for  (for example) JAVA, Python, or C#. or server side in BASIC, C or Python? The UniDynArray is a client-side object so I suspect the former.

    Please let us know the context and the language environment. If it's C# then this might help which uses Json.NET:

    https://stackoverflow.com/questions/42639545/how-to-convert-my-c-sharp-array-to-json

    Regards

    JJ



    ------------------------------
    John Jenkins
    Thame, Oxfordshire
    ------------------------------



  • 3.  RE: UniDynArray in Visual Studio C#

    Posted 08-02-2023 16:54

    Hi John,

    Thanks for the reply, (FYI - i worked with you before in South Africa)

    I would prefer to do it client side C#, as well if i can do it server side, but i was not sure if there was a converter in the pgm side?



    ------------------------------
    Daniel Conradie
    Rocket Forum Shared Account
    ------------------------------



  • 4.  RE: UniDynArray in Visual Studio C#

    PARTNER
    Posted 08-02-2023 09:54

    Daniel,

    As John J said, using a json serializer is the easiest way to proceed.  The most common one is from Newtonsoft but Microsoft has one also.   If you are using Visual Studio then you can be install one of these into your C# project using package manager or you can use NuGET.  That is the easy part.  The more difficult task is creating the schema/structure of the JSON.  If you are talking about a fixed schema then you can create a class and convert the UniDynArray to an object and then convert that object to JSON which is really just a string. Your other option is to just manually create the JSON string but this is a bit more cumbersome and code intensive.

    If you want to convert the DynamicArray to JSON before returning it to your C# program, there are a couple of Github sites that have Pick subroutines to manage JSON.  Again, the JSON is just a string to Pick and also to C#.  For example, on Github there is:

    https://github.com/Krowemoh/TCL-Utilities and https://github.com/patrickp/wobj

    It just depends on how you want to approach it.

    Dick Thiot

    Maverick Systems



    ------------------------------
    Richard Thiot
    President
    Maverick Systems
    Plano TX US
    ------------------------------



  • 5.  RE: UniDynArray in Visual Studio C#

    Posted 08-02-2023 16:58

    Thanks for this Richard, 
    Before returning it to the C# program most likely will be easier.

    Its just on the C# i am concerned how it handles the multivalve in the json side.

    I will look at these examples on GITHub, if you have any more example please send, the more i look there better idea i can get.

    Thank you once again.



    ------------------------------
    Daniel Conradie
    Rocket Forum Shared Account
    ------------------------------



  • 6.  RE: UniDynArray in Visual Studio C#

    PARTNER
    Posted 08-03-2023 12:30
    Daniel,

    If you want to post some specifics I am happy to help. Also, remember that a Pick dynamic array is just a string to C#. This can be easily converted to an array with the Split method. You can split on CHAR(254) (attribute mark) for example. If you know the structure of the dynamic array you can convert it to an object which then you are able to manipulate in many ways in C#.

    Dick




  • 7.  RE: UniDynArray in Visual Studio C#

    Posted 08-07-2023 04:08

    Hi,
    Please could i still try get help, can show you and example.

          OPEN 'DATA' TO IFILE ELSE STOP
    FLD=''
    *
          READ DATA FROM IFILE, 'KEY1' ELSE GO 999
    FLD<1>=DATA<1>
    FLD<2>=DATA<5>
    FLD<3,1>=DATA<6,1>
    FLD<4,1>=DATA<7,1>
    etc....
    This is reading from a Universe table with multivalues, just the FLD i would like to return as a JSON, as the FLD will be part of the tables data, and there is no dict for this (unless i need that, then fine, could add it)


    ------------------------------
    Daniel Conradie
    Rocket Forum Shared Account
    ------------------------------



  • 8.  RE: UniDynArray in Visual Studio C#
    Best Answer

    ROCKETEER
    Posted 08-07-2023 05:58

    Daniel,

    There are a couple of ways to get things in JSON format in UniVerse. If you take a look in the Basic Extensions Manual there is a subject covering U2 Dynamic Objects which are based on JSON. Reterive also supports the TOJSON keyword which is a quick way to get JSON output, you could utliize this in your program using an EXECUTE CAPTURING and then parsing the output.

    So for example

    LIST CUSTOMER "2" FNAME LNAME TOJSON

    Gives you

    {
    "CUSTOMER":[
     {"_ID" : "2","FNAME" : "Diana","LNAME" : "Morris"
     }
    ]
    }

    For multi-valued fields

    LIST CUSTOMER "2" ORDERS TOJSON

    {
    "CUSTOMER":[
     {"_ID" : "2",
      "ORDERS_MV":[
        {"PRODID" : "C2000","DESCRIPTION" : "Moderate duty, entry level, color copie
    r","SER_NUM" : "600782","BUY_DATE" : "01/08/91","PAID_DATE" : "02/05/91","LIST_P
    RICE" : "$6,890","PRICE" : "$6,600","DISCOUNT" : "4.2","SVC_START" : "01/13/91",
    "SVC_END" : "01/15/92","SVC_PRICE" : "$900","SVC_PAID_DATE" : "02/05/91"
        },
        {"PRODID" : "M3000","DESCRIPTION" : "Heavy duty monochrome copier","SER_NUM"
     : "700422","BUY_DATE" : "01/08/91","PAID_DATE" : "02/05/91","LIST_PRICE" : "$12
    ,990","PRICE" : "$12,000","DISCOUNT" : "7.6","SVC_START" : "01/13/91","SVC_END"
    : "06/12/91","SVC_PRICE" : "$500","SVC_PAID_DATE" : "02/05/91"
        },
        {"PRODID" : "S3000","DESCRIPTION" : "Sorting attachment for M3000/C3000","SE
    R_NUM" : "101456","BUY_DATE" : "01/22/91","PAID_DATE" : "02/12/91","LIST_PRICE"
    : "$1,990","PRICE" : "$900","DISCOUNT" : "54.8","SVC_START" : "01/13/91","SVC_EN
    D" : "01/15/92","SVC_PRICE" : "$150","SVC_PAID_DATE" : "02/12/91"
        }
       ]
     }

    Regards,



    ------------------------------
    Jonathan Smith
    UniData ATS
    Rocket Support
    ------------------------------



  • 9.  RE: UniDynArray in Visual Studio C#

    PARTNER
    Posted 08-07-2023 09:16

    Daniel,

    Jonathan has very valuable advice.  In order to do that in your BASIC program you would not need to explicitly READ the record but could do the following:

    SENT = LIST DATA FLD1 FLD2 FLD3.1 FLD4.1 TOJSON'

    EXECUTE SENT CAPTURING JSON.DATA

    In this example, you need DICT records for FLD1, FLD2, FLD3.1 and FLD4.1 in DATA.  You likely already have these.

    I hope that this helps.

    Dick



    ------------------------------
    Richard Thiot
    President
    Maverick Systems
    Plano TX US
    ------------------------------



  • 10.  RE: UniDynArray in Visual Studio C#

    ROCKETEER
    Posted 08-07-2023 09:37

    Daniel,

    Final little tip ... you should also be able to do this ... LIST CUSTOMER "2" LNAME FNAME TOJSON TO CUST.2 and the JSON output is written to the file CUST.2.json in the &XML& file. However, when I tested this internally it works for UniData but not for UniVerse, so may be a bug on the 11.3.5 version of UniVerse I was testing on.

    Regards,



    ------------------------------
    Jonathan Smith
    UniData ATS
    Rocket Support
    ------------------------------