Home > Mobile >  Mysql json binary encoding
Mysql json binary encoding

Time:12-06

Does MySQL use bson for its json-encoding? Or does it have a custom binary encoding? For example:

Optimized storage format. JSON documents stored in JSON columns are converted to an internal format that permits quick read access to document elements. When the server later must read a JSON value stored in this binary format, the value need not be parsed from a text representation. The binary format is structured to enable the server to look up subobjects or nested values directly by key or array index without reading all values before or after them in the document.

https://dev.mysql.com/doc/refman/8.0/en/json.html

CodePudding user response:

The internal encoding for JSON in MySQL is not literally BSON.

https://elephantdolphin.blogspot.com/2019/01/mysql-shell-8014-now-with-bson.html says:

The import utility can process documents that use JSON extensions to represent BSON data types, convert them to an identical or compatible MySQL representation, and import the data value using that representation.

The message of this blog post is that BSON needs to be converted to be stored in MySQL.

But in some ways the binary encoding of MySQL's JSON has similar goals as BSON.

MySQL's JSON encoding converts some data types to binary representations instead of strings. Also, objects and arrays each contain a kind of "table of contents" for their keys and values.

You can read more about the MySQL JSON encoding in the source here:

https://github.com/mysql/mysql-server/blob/8.0/sql/json_binary.h

All that said, there's no reason you need to know the encoding of binary JSON extensions in MySQL, because textual JSON is converted into binary on insert, and converted back to text on fetch.

  • Related