Invalid JSON Parsing

SuiteScript can parse JSON strings that you use in your scripts. When invalid JSON is encountered, a syntax error is generated. The type and format of the generated error message is different in SuiteScript 2.1 than in SuiteScript 2.0. Also, including trailing commas in JSON strings is accepted in SuiteScript 2.0 but generates an error in SuiteScript 2.1.

Scenario

SuiteScript 2.0 Behavior

SuiteScript 2.1 Behavior

Parse the following JSON string in a script:

                    var obj = 'b';
JSON.parse(obj); 

                  

The following error is generated:

org.mozilla.javascript.EcmaError: SyntaxError: Unexpected token: b

The following (or similar) error is generated:

SyntaxError: Invalid JSON: <json>:1:0 Expected json literal but found b

Parse the following JSON string in a script:

                    JSON.parse('{"mykey" : 1, }'); 

                  

The script executes successfully.

The following (or similar) error is generated:

SyntaxError: Invalid JSON: <json>:1:0 Expected json literal but found ,

To avoid this issue, make sure you use the JSON format that is compatible with the ECMAScript specification. For more information, see JSON object.

Related Topics

General Notices