Expand description
jsonb is a binary format JSON representation inspired by PostgreSQL and CockroachDB. It provides a fast, lightweight and easy-to-use API for working with JSON data.
Features
- Good compatibility:
jsonbfully supports theJSONstandard and can be used to store complex data structures. - Fast performance:
jsonbis designed for high performance, allowing you to work with largeJSONdata sets with ease. - Easy to use:
jsonbprovides a number of built-in functions to support various operations, and also supports theJSONPathsyntax for selecting and extracting subset elements. - Safe and secure:
jsonbis written in Rust, which provides memory and thread safety guarantees, making it a safe choice for handling sensitive data.
Encoding format
The jsonb encoding format is a tree-like structure. Each node contains a container header, a number of JEntry headers, and nested encoding values.
- 32-bit container header. 3 bits identify the type of value, including
scalar,objectandarray, and 29 bits identify the number of JEntries in thearrayorobject. The root node of thejsonbvalue is always a container header.scalarcontainer header:0x20000000objectcontainer header:0x40000000arraycontainer header:0x80000000
- 32-bit JEntry header. 1 bit identifies whether the JEntry stores a length or an offset, 3 bits identify the type of value, including
null,string,number,false,trueandcontainer, and the remaining 28 bits identify the length or offset of the encoding value.nullJEntry header:0x00000000stringJEntry header:0x10000000numberJEntry header:0x20000000falseJEntry header:0x30000000trueJEntry header:0x40000000containerJEntry header0x50000000
- Encoding value. Different types of JEntry header have different encoding values.
null,true,false: no encoding value, identified by the JEntry header.string: a normal UTF-8 string.number: an encoded number to represent uint64s, int64s and float64s.container: a nestedjsonvalue with a recursive structure.
An encoding example
// JSON value
[false, 10, {"k":"v"}]
// JSONB encoding
0x80000003 array container header (3 JEntries)
0x30000000 false JEntry header (no encoding value)
0x20000002 number JEntry header (encoding value length 2)
0x5000000e container JEntry header (encoding value length 14)
0x500a number encoding value (10)
0x40000001 object container header (1 JEntry)
0x10000001 string key JEntry header (encoding value length 1)
0x10000001 string value JEntry header (encoding value length 1)
0x6b string encoding value ("k")
0x76 string encoding value ("v")
Modules
Enums
Functions
- Get the length of
JSONBarray. - Convert the values of a
JSONBarray to vector. - If the
JSONBis a Boolean, returns the associated bool. Returns None otherwise. - If the
JSONBis a Number, represent it as f64 if possible. Returns None otherwise. - If the
JSONBis a Number, represent it as i64 if possible. Returns None otherwise. - If the
JSONBis a Null, returns (). Returns None otherwise. - If the
JSONBis a Number, returns the Number. Returns None otherwise. - If the
JSONBis a String, returns the String. Returns None otherwise. - If the
JSONBis a Number, represent it as u64 if possible. Returns None otherwise. - Build
JSONBarray from items. Assuming that the input values is valid JSONB data. - Build
JSONBobject from items. Assuming that the input values is valid JSONB data. JSONBvalues supports partial decode for comparison, if the values are found to be unequal, the result will be returned immediately. In first level header, values compare as the following order: Scalar Null > Array > Object > Other Scalars(String > Number > Boolean).- Convert
JSONBvalue to comparable vector. The compare rules are the same as thecomparefunction. Scalar Null > Array > Object > Other Scalars(String > Number > Boolean). - The binary
JSONBcontains three parts,Header,JEntryandRawData. This structure can be nested. Each group of structures starts with aHeader. The upper-levelValuewill store theHeaderlength or offset of the lower-levelValue.Headerstores the type of theValue, includeArray,ObjectandScalar,Scalarhas only oneValue, and a correspondingJEntry.ArrayandObjectare nested type, they have multiple lower-levelValues. So theHeaderalso stores the number of lower-levelValues.JEntrystores the types ofScalar Value, includingNull,True,False,Number,StringandContainer. They have three different decode methods. - Get the inner element of
JSONBArray by index. - Get the inner element of
JSONBObject by key name, ifignore_caseis true, enables case-insensitive matching. - Get the inner elements of
JSONBvalue by JSON path. The return value may contains multiple matching elements. - Get the inner elements of
JSONBvalue by JSON path. If there are multiple matching elements, return anJSONBArray. - Get the inner element of
JSONBvalue by JSON path. If there are multiple matching elements, only the first one is returned - Returns true if the
JSONBis An Array. Returns false otherwise. - Returns true if the
JSONBis a Boolean. Returns false otherwise. - Returns true if the
JSONBis a f64 Number. Returns false otherwise. - Returns true if the
JSONBis a i64 Number. Returns false otherwise. - Returns true if the
JSONBis a Null. - Returns true if the
JSONBis a Number. Returns false otherwise. - Returns true if the
JSONBis An Object. Returns false otherwise. - Returns true if the
JSONBis a String. Returns false otherwise. - Returns true if the
JSONBis a u64 Number. Returns false otherwise. - Convert the values of a
JSONBobject to vector of key-value pairs. - Get the keys of a
JSONBobject. - Checks whether the JSON path returns any item for the
JSONBvalue. - generate random JSONB value
- Deletes all object fields that have null values from the given JSON value, recursively. Null values that are not object fields are untouched.
- Cast
JSONBvalue to Boolean - Cast
JSONBvalue to f64 - Cast
JSONBvalue to i64 - Convert
JSONBvalue to pretty String - Cast
JSONBvalue to String - Convert
JSONBvalue to String - Cast
JSONBvalue to u64 - Traverse all the string fields in a jsonb value and check whether the conditions are met.
- Returns the type of the top-level JSON value as a text string. Possible types are object, array, string, number, boolean, and null.