cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
0 Kudos

Parse JSON error handling

Parse JSON() is hard to use because it does not handle errors nicely. In a callback, perhaps from RunProgram or a button, it will create an unwanted model dialog announcing the failure. It will also announce the failure in the log. It takes extra JSL to fix it up:

New Window( "parsejson",
    Button Box( "parse",
        local( {birestore, errText},
            birestore = Batch Interactive( 1 ); // makes an alert dialog from a button without this
            errText = Log Capture( p = Parse JSON( "\!n" /* invalid json error */ ) ); // writes to the log without this
            Batch Interactive( birestore ); // pop the previous behavior
            If( Length( errText ), Throw( "Caught an error:" || char(errText) ) ); // handle unexpected error, somehow
            /* use p here */
        )
    )
);

Suggestion: add an optional parameter to change the behavior, something like parseJson(x, throwIfError() )

Failsafe JSON parsing