ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Data-Into parse JSon without document root

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Data-Into parse JSon without document root

    Hi all,

    Could I parse a JSon without document root?

    This is my RPGLE:

    Code:
    Ctl-Opt DftActGrp(*No) ActGrp('QILE')
    BndDir( 'AL400MNUV2')
    DatFmt(*Iso) TimFmt(*Iso)
    Alwnull(*UsrCtl)
    DftName(TESTJSON)
    Text('ETMS - Invio richiesta di Margine')
    Option(*SrcStmt:*NoDebugIo :*NoUnRef)
    Debug;
    dcl-s jsondata varchar(10000) inz('+
    { "ClientRequestId" : 12345 , "client": "43535", "requesttype": "O" , "orders": [ +
    { "instrument": "8L1890", "quantity": 10, "price": 296 , "side": "C" } , +
    { "instrument": "8L1910", "quantity": 5, "price": 100 , "side":"B" } +
    ] }');
    dcl-ds ClientRequestId Qualified ;
      ClientRequestId Int(10) ;
      client Char(5) ;
      requesttype Char(1) ;
      count_orders int(5) ;
      Dcl-ds orders Dim(99) ;
        instrument varChar(6) ;
        quantity packed(10 :0) ;
        price packed(15 :5) ;
        side Char(1) ;
      End-Ds orders;
    End-Ds ClientRequestId;
    
    dcl-ds *N psds;
      count Int(20) Pos(372);
    end-ds;
    
    DATA-INTO ClientRequestId %Data(jsonData:
    'case=any doc=string countprefix=count_ allowmissing=yes' )
    %Parser('JSONPARSE');
    
    *InLR = *On;

    Many thanks.

  • #2
    I apologies if you already know most of this.

    DATA-INTO is not itself a JSON parser, it can be sued to invoke a separate parser program specified with %PARSER(). In your example you are using program JSONPARSE, which is the JSON parser provided by IBM along with DATA-INTO.

    IBM have said that JSONPARSE should NOT be used for production systems, it is intended only as an example on how to write a parser. As a result it has a number of limitations, one of which is (I think) that it cannot handle the case where the root node has no name.

    If you want a more comprehensive JSON parser, then you should look at YAJL: https://www.scottklement.com/yajl/
    YAJL is a JSON Library that the legendary Scott Klement ported to iSeries. The most recent version includes a DATA-INTO JSON parser that is far more capable than the IBM provided parser.

    (Disclaimer - I do not know for certain if YAJL can handle your specific case, I only know that the IBM parser has limitations that YAJL does not).

    Comment


    • #3
      There are two easy ways to handle this. You could modify the IBM supplied example as described in this thread https://archive.midrange.com/rpg400-.../msg00135.html but as Vectorspace has pointed out - this was never intended by IBM to be production code and it has other issues that you have yet to discover!

      The better option is to use Scott's YAJLINTO which you can get from the link that Vectorspace supplied. That would be my preference and also gives you the whole YAJL tool set which you can use to consume JSON in circumstances where DATA-INTO doesn't work well, and also give you the tools to generate JSON too.

      Comment


      • #4
        Thank you.
        I have download the YAJL parse.
        Many thanks.
        Bye

        Comment

        Working...
        X