Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/doc/jsonb.md # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / doc / jsonb.md` 289 lines ยท 13940 bytes Raw Blame History Rendered 1 # The JSONB Format 2 3 This document describes SQLite's JSONB binary encoding of 4 JSON. 5 6 ## 1.0 What Is JSONB? 7 8 Beginning with version 3.45.0 (circa 2024-01-01), SQLite supports an 9 alternative binary encoding of JSON which we call "JSONB". JSONB is 10 a binary format that stored as a BLOB. 11 12 The advantage of JSONB over ordinary text RFC 8259 JSON is that JSONB 13 is both slightly smaller (by between 5% and 10% in most cases) and 14 can be processed in less than half the number of CPU cycles. The built-in 15 [JSON SQL functions] of SQLite can accept either ordinary text JSON 16 or the binary JSONB encoding for any of their JSON inputs. 17 18 The "JSONB" name is inspired by [PostgreSQL](https://postgresql.org), but the 19 on-disk format for SQLite's JSONB is not the same as PostgreSQL's. 20 The two formats have the same name, but they have wildly different internal 21 representations and are not in any way binary compatible. 22 23 The central idea behind this JSONB specification is that each element 24 begins with a header that includes the size and type of that element. 25 The header takes the place of punctuation such as double-quotes, 26 curly-brackes, square-brackets, commas, and colons. Since the size 27 and type of each element is contained in its header, the element can 28 be read faster since it is no longer necessary to carefully scan forward 29 looking for the closing delimiter. The payload of JSONB is the same 30 as for corresponding text JSON. The same payload bytes occur in the 31 same order. The only real difference between JSONB and ordinary text 32 JSON is that JSONB includes a binary header on 33 each element and omits delimiter and separator punctuation. 34 35 ### 1.1 Internal Use Only 36 37 The details of the JSONB are not intended to be visible to application 38 developers. Application developers should look at JSONB as an opaque BLOB 39 used internally by SQLite. Nevertheless, we want the format to be backwards 40 compatible across all future versions of SQLite. To that end, the format 41 is documented by this file in the source tree. But this file should be 42 used only by SQLite core developers, not by developers of applications 43 that only use SQLite. 44 45 ## 2.0 The Purpose Of This Document 46 47 JSONB is not intended as an external format to be used by 48 applications. JSONB is designed for internal use by SQLite only. 49 Programmers do not need to understand the JSONB format in order to 50 use it effectively. 51 Applications should access JSONB only through the [JSON SQL functions], 52 not by looking at individual bytes of the BLOB. 53 54 However, JSONB is intended to be portable and backwards compatible 55 for all future versions of SQLite. In other words, you should not have 56 to export and reimport your SQLite database files when you upgrade to 57 a newer SQLite version. For that reason, the JSONB format needs to 58 be well-defined. 59 60 This document is therefore similar in purpose to the 61 [SQLite database file format] document that describes the on-disk 62 format of an SQLite database file. Applications are not expected 63 to directly read and write the bits and bytes of SQLite database files. 64 The SQLite database file format is carefully documented so that it 65 can be stable and enduring. In the same way, the JSONB representation 66 of JSON is documented here so that it too can be stable and enduring, 67 not so that applications can read or writes individual bytes. 68 69 ## 3.0 Encoding 70 71 JSONB is a direct translation of the underlying text JSON. The difference 72 is that JSONB uses a binary encoding that is faster to parse compared to 73 the detailed syntax of text JSON. 74 75 Each JSON element is encoded as a header and a payload. The header 76 determines type of element (string, numeric, boolean, null, object, or 77 array) and the size of the payload. The header can be between 1 and 78 9 bytes in size. The payload can be any size from zero bytes up to the 79 maximum allowed BLOB size. 80 81 ### 3.1 Payload Size 82 83 The upper four bits of the first byte of the header determine size of the 84 header and possibly also the size of the payload. 85 If the upper four bits have a value between 0 and 11, then the header is 86 exactly one byte in size and the payload size is determined by those 87 upper four bits. If the upper four bits have a value between 12 and 15, 88 that means that the total header size is 2, 3, 5, or 9 bytes and the 89 payload size is unsigned big-endian integer that is contained in the 90 subsequent bytes. The size integer is the one byte that following the 91 initial header byte if the upper four bits 92 are 12, two bytes if the upper bits are 13, four bytes if the upper bits 93 are 14, and eight bytes if the upper bits are 15. The current design 94 of SQLite does not support BLOB values larger than 2GiB, so the eight-byte 95 variant of the payload size integer will never be used by the current code. 96 The eight-byte payload size integer is included in the specification 97 to allow for future expansion. 98 99 The header for an element does *not* need to be in its simplest 100 form. For example, consider the JSON numeric value "`1`". 101 That element can be encode in five different ways: 102 103 * `0x13 0x31` 104 * `0xc3 0x01 0x31` 105 * `0xd3 0x00 0x01 0x31` 106 * `0xe3 0x00 0x00 0x00 0x01 0x31` 107 * `0xf3 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x01 0x31` 108 109 The shortest encoding is preferred, of course, and usually happens with 110 primitive elements such as numbers. However the total size of an array 111 or object might not be known exactly when the header of the element is 112 first generated. It is convenient to reserve space for the largest 113 possible header and then go back and fill in the correct payload size 114 at the end. This technique can result in array or object headers that 115 are larger than absolutely necessary. 116 117 ### 3.2 Element Type 118 119 The least-significant four bits of the first byte of the header (the first 120 byte masked against 0x0f) determine element type. The following codes are 121 used: 122 123
NULL → 125 The element is a JSON "null". The payload size for a true JSON NULL must 126 must be zero. Future versions of SQLite might extend the JSONB format 127 with elements that have a zero element type but a non-zero size. In that 128 way, legacy versions of SQLite will interpret the element as a NULL 129 for backwards compatibility while newer versions will interpret the 130 element in some other way. 131 132
TRUE → 133 The element is a JSON "true". The payload size must be zero for a actual 134 "true" value. Elements with type 1 and a non-zero payload size are 135 reserved for future expansion. Legacy implementations that see an element 136 type of 1 with a non-zero payload size should continue to interpret that 137 element as "true" for compatibility. 138 139
FALSE → 140 The element is a JSON "false". The payload size must be zero for a actual 141 "false" value. Elements with type 2 and a non-zero payload size are 142 reserved for future expansion. Legacy implementations that see an element 143 type of 2 with a non-zero payload size should continue to interpret that 144 element as "false" for compatibility. 145 146
INT → 147 The element is a JSON integer value in the canonical 148 RFC 8259 format, without extensions. The payload is the ASCII 149 text representation of that numeric value. 150 151
INT5 → 152 The element is a JSON integer literal in hexadecimal notation. 153 The payload is the ASCII text representation of the literal. 154 Because the payload is in a non-standard format, it will need 155 to be translated when the JSONB is converted into RFC 8259 text JSON. 156 157
FLOAT → 158 The element is a JSON floating-point value in the canonical 159 RFC 8259 format, without extensions. The payload is the ASCII 160 text representation of that numeric value. 161 162
FLOAT5 → 163 The element is a JSON floating-point value that is not in the 164 canonical JSON format but rather the extended JSON5 format. 165 The payload is the ASCII text representation of that numeric value. 166 Because the payload is in a non-standard format, it will need to 167 be translated when the JSONB is converted into RFC 8259 text JSON. 168 169
TEXT → 170 The element is a JSON string value that does not contain 171 any escapes nor any characters that need to be escaped for either SQL or 172 JSON. The payload is the UTF8 text representation of the string value. 173 The payload does not include string delimiters. 174 175
TEXTJ → 176 The element is a JSON string value that contains 177 RFC 8259 character escapes (such as "\n" or "\u0020"). 178 Those escapes will need to be translated into actual UTF8 if this element 179 is [json_extract|extracted] into SQL. 180 The payload is the UTF8 text representation of the escaped string value. 181 The payload does not include string delimiters. 182 183
TEXT5 → 184 The element is a JSON string value that contains 185 character escapes, including some character escapes that part of JSON5 186 and which are not found in the canonical RFC 8259 spec. 187 Those escapes will need to be translated into standard JSON prior to 188 rendering the JSON as text, or into their actual UTF8 characters if this 189 element is [json_extract|extracted] into SQL. 190 The payload is the UTF8 text representation of the escaped string value. 191 The payload does not include string delimiters. 192 193
TEXTRAW → 194 The element is a JSON string value that contains 195 UTF8 characters that need to be escaped if this string is rendered into 196 standard JSON text. 197 The payload does not include string delimiters. 198 199
ARRAY → 200 The element is a JSON array. The payload contains 201 JSONB elements that comprise values contained within the array. 202 203
OBJECT → 204 The element is a JSON object. The payload contains 205 pairs of JSONB elements that comprise entries for the JSON object. 206 The first element in each pair must be a string (types 7 through 10). 207 The second element of each pair may be any types, including nested 208 arrays or objects. 209 210
RESERVED-13 → 211 Reserved for future expansion. Legacy implements that encounter this 212 element type should raise an error. 213 214
RESERVED-14 → 215 Reserved for future expansion. Legacy implements that encounter this 216 element type should raise an error. 217 218
RESERVED-15 → 219 Reserved for future expansion. Legacy implements that encounter this 220 element type should raise an error. 221