Using noxDB, ParseString fails, but ParseFile is successful with the same JSON

49 Views Asked by At

Using noxDB on the IBM i in an RPG program, ParseString fails, but ParseFile is successful with the same JSON.

Please see the example below. Is ParseString unable to handle arrays? If so, is there a workaround?

In example1, json_Error(pJson) returns true. Here is the error message:

Invalid token at (1:101) token number: 5. Was expecting an ',' but got a { near

In example2, json_Error(pJson) returns false.

// ------------------------------------------------------------------------------------
// example1
// ------------------------------------------------------------------------------------
Dcl-Proc example1;
 
    Dcl-S pJson       Pointer;
    Dcl-S msg         varchar(256);
 
    pJson = json_parseString('            -
      {                                   -
    "people":[                        -
        {                         -
            "name":"John",    -
            "age":23,         -
        },                        -
         {                                -
            "name":"David",   -
            "age":25,         -
        }                         -
    ]                                 -
      }                                   -
    ');
 
    If json_Error(pJson) ;
        msg = json_Message(pJson);
        myTrace (msg :pJson);
        json_delete(pJson);
        Return;
    EndIf;
 
    myTrace ('After the parse: ':pJson);
 
    json_delete (pJson);
 
End-Proc;
// ------------------------------------------------------------------------------------
// example2
// ------------------------------------------------------------------------------------
Dcl-Proc example2;
 
    Dcl-S pJson       Pointer;
    Dcl-S msg         varchar(256);
 
    pJson = json_ParseFile ('./example2.json');
    If json_Error(pJson) ;
        msg = json_Message(pJson);
        myTrace (msg :pJson);
        json_delete(pJson);
        Return;
    EndIf;
 
    myTrace ('After the parse: ':pJson);
 
    json_delete (pJson);
 
End-Proc;

Thank you for your help.

1

There are 1 best solutions below

4
Charles On

You don't include the stream file, but I suspect something is different...

I've not used noxDb, but I wouldn't expect json_parseString() to work any different than json_ParseFile(). Not sure if anyone here on SO is could to have any experience with noxDb. You might try the Midrange RPG mailing list or the noxDB github. Actually, looking at the source of noxDb json_ParseFile simply reads the entire file and passes it to json_ParseString(). So yeah..something is different.

Position 101 is the opening { for the first person as far as I can tell.

I'd recommend declaring a string variable and passing that into json_parseString() rather than the constant value you have now. That will allow to to inspect the value via debug or just with a DSPLY.

EDIT
Note that the , after age is technically not valid. Though some JSON parsers don't seem to care.

Personally, I stay away from continuing literals across lines. I'd do it this way.

jsonData 
  = '{'
     + '"people":['
     +       '{"name":"John","age":23},'
     +       '{"name":"David","age":25}'
     +    ']'
     + '}';

pJson = json_parseString(jsonData);