JSON_TO_BSON scalar function

The JSON_TO_BSON function converts a string that contains data that is formatted for JSON to a binary string that contains data that is formatted as BSON.

Read syntax diagramSkip visual syntax diagramJSON_TO_BSON(JSON-expression )

Although the schema for this function is SYSIBM, the function cannot be specified as a qualified name.

JSON-expression
Specifies an expression that returns a character string value. It must contain formatted JSON data (SQLSTATE 22032).

If JSON-expression can be null, the result can be null; if JSON-expression is null, the result is the null value.

Notes

  • If parameter markers are not explicitly cast to a supported data type, an error is returned (SQLSTATE 42815)

Examples

  1. Convert a valid JSON document to BSON format and insert it into in a table.
    INSERT INTO TESTJSON VALUES (JSON_TO_BSON('{"Name":"George"}'));
    

    This example inserts x'16000000024E616D65000700000047656F7267650000' into the table named TESTJSON.

  2. Convert an incorrectly formatted JSON document, which is missing a value for the key "Name".
    INSERT INTO TESTJSON VALUES (JSON_TO_BSON('{"Name":, "Age" : 32}'));
    
    SQL16402N  JSON data is not valid.  SQLSTATE=22032

    This example results in an error.