interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / doc / jsonb.md

289 lines · 13940 bytes Raw Blame History Rendered
1# The JSONB Format
2
3This document describes SQLite's JSONB binary encoding of
4JSON.
5
6## 1.0 What Is JSONB?
7
8Beginning with version 3.45.0 (circa 2024-01-01), SQLite supports an
9alternative binary encoding of JSON which we call "JSONB". JSONB is
10a binary format that stored as a BLOB.
11
12The advantage of JSONB over ordinary text RFC 8259 JSON is that JSONB
13is both slightly smaller (by between 5% and 10% in most cases) and
14can 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
16or the binary JSONB encoding for any of their JSON inputs.
17
18The "JSONB" name is inspired by [PostgreSQL](https://postgresql.org), but the
19on-disk format for SQLite's JSONB is not the same as PostgreSQL's.
20The two formats have the same name, but they have wildly different internal
21representations and are not in any way binary compatible.
22
23The central idea behind this JSONB specification is that each element
24begins with a header that includes the size and type of that element.
25The header takes the place of punctuation such as double-quotes,
26curly-brackes, square-brackets, commas, and colons. Since the size
27and type of each element is contained in its header, the element can
28be read faster since it is no longer necessary to carefully scan forward
29looking for the closing delimiter. The payload of JSONB is the same
30as for corresponding text JSON. The same payload bytes occur in the
31same order. The only real difference between JSONB and ordinary text
32JSON is that JSONB includes a binary header on
33each element and omits delimiter and separator punctuation.
34
35### 1.1 Internal Use Only
36
37The details of the JSONB are not intended to be visible to application
38developers. Application developers should look at JSONB as an opaque BLOB
39used internally by SQLite. Nevertheless, we want the format to be backwards
40compatible across all future versions of SQLite. To that end, the format
41is documented by this file in the source tree. But this file should be
42used only by SQLite core developers, not by developers of applications
43that only use SQLite.
44
45## 2.0 The Purpose Of This Document
46
47JSONB is not intended as an external format to be used by
48applications. JSONB is designed for internal use by SQLite only.
49Programmers do not need to understand the JSONB format in order to
50use it effectively.
51Applications should access JSONB only through the [JSON SQL functions],
52not by looking at individual bytes of the BLOB.
53
54However, JSONB is intended to be portable and backwards compatible
55for all future versions of SQLite. In other words, you should not have
56to export and reimport your SQLite database files when you upgrade to
57a newer SQLite version. For that reason, the JSONB format needs to
58be well-defined.
59
60This document is therefore similar in purpose to the
61[SQLite database file format] document that describes the on-disk
62format of an SQLite database file. Applications are not expected
63to directly read and write the bits and bytes of SQLite database files.
64The SQLite database file format is carefully documented so that it
65can be stable and enduring. In the same way, the JSONB representation
66of JSON is documented here so that it too can be stable and enduring,
67not so that applications can read or writes individual bytes.
68
69## 3.0 Encoding
70
71JSONB is a direct translation of the underlying text JSON. The difference
72is that JSONB uses a binary encoding that is faster to parse compared to
73the detailed syntax of text JSON.
74
75Each JSON element is encoded as a header and a payload. The header
76determines type of element (string, numeric, boolean, null, object, or
77array) and the size of the payload. The header can be between 1 and
789 bytes in size. The payload can be any size from zero bytes up to the
79maximum allowed BLOB size.
80
81### 3.1 Payload Size
82
83The upper four bits of the first byte of the header determine size of the
84header and possibly also the size of the payload.
85If the upper four bits have a value between 0 and 11, then the header is
86exactly one byte in size and the payload size is determined by those
87upper four bits. If the upper four bits have a value between 12 and 15,
88that means that the total header size is 2, 3, 5, or 9 bytes and the
89payload size is unsigned big-endian integer that is contained in the
90subsequent bytes. The size integer is the one byte that following the
91initial header byte if the upper four bits
92are 12, two bytes if the upper bits are 13, four bytes if the upper bits
93are 14, and eight bytes if the upper bits are 15. The current design
94of SQLite does not support BLOB values larger than 2GiB, so the eight-byte
95variant of the payload size integer will never be used by the current code.
96The eight-byte payload size integer is included in the specification
97to allow for future expansion.
98
99The header for an element does *not* need to be in its simplest
100form. For example, consider the JSON numeric value "`1`".
101That 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
109The shortest encoding is preferred, of course, and usually happens with
110primitive elements such as numbers. However the total size of an array
111or object might not be known exactly when the header of the element is
112first generated. It is convenient to reserve space for the largest
113possible header and then go back and fill in the correct payload size
114at the end. This technique can result in array or object headers that
115are larger than absolutely necessary.
116
117### 3.2 Element Type
118
119The least-significant four bits of the first byte of the header (the first
120byte masked against 0x0f) determine element type. The following codes are
121used:
122
123<ol>
124<li type="0"><p><b>NULL</b> &rarr;
125The element is a JSON "null". The payload size for a true JSON NULL must
126must be zero. Future versions of SQLite might extend the JSONB format
127with elements that have a zero element type but a non-zero size. In that
128way, legacy versions of SQLite will interpret the element as a NULL
129for backwards compatibility while newer versions will interpret the
130element in some other way.
131
132<li value="1"><p><b>TRUE</b> &rarr;
133The 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
135reserved for future expansion. Legacy implementations that see an element
136type of 1 with a non-zero payload size should continue to interpret that
137element as "true" for compatibility.
138
139<li value="2"><p><b>FALSE</b> &rarr;
140The 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
142reserved for future expansion. Legacy implementations that see an element
143type of 2 with a non-zero payload size should continue to interpret that
144element as "false" for compatibility.
145
146<li value="3"><p><b>INT</b> &rarr;
147The element is a JSON integer value in the canonical
148RFC 8259 format, without extensions. The payload is the ASCII
149text representation of that numeric value.
150
151<li value="4"><p><b>INT5</b> &rarr;
152The element is a JSON integer literal in hexadecimal notation.
153The payload is the ASCII text representation of the literal.
154Because the payload is in a non-standard format, it will need
155to be translated when the JSONB is converted into RFC 8259 text JSON.
156
157<li value="5"><p><b>FLOAT</b> &rarr;
158The element is a JSON floating-point value in the canonical
159RFC 8259 format, without extensions. The payload is the ASCII
160text representation of that numeric value.
161
162<li value="6"><p><b>FLOAT5</b> &rarr;
163The element is a JSON floating-point value that is not in the
164canonical JSON format but rather the extended JSON5 format.
165The payload is the ASCII text representation of that numeric value.
166Because the payload is in a non-standard format, it will need to
167be translated when the JSONB is converted into RFC 8259 text JSON.
168
169<li value="7"><p><b>TEXT</b> &rarr;
170The element is a JSON string value that does not contain
171any escapes nor any characters that need to be escaped for either SQL or
172JSON. The payload is the UTF8 text representation of the string value.
173The payload does <i>not</i> include string delimiters.
174
175<li value="8"><p><b>TEXTJ</b> &rarr;
176The element is a JSON string value that contains
177RFC 8259 character escapes (such as "<tt>\n</tt>" or "<tt>\u0020</tt>").
178Those escapes will need to be translated into actual UTF8 if this element
179is [json_extract|extracted] into SQL.
180The payload is the UTF8 text representation of the escaped string value.
181The payload does <i>not</i> include string delimiters.
182
183<li value="9"><p><b>TEXT5</b> &rarr;
184The element is a JSON string value that contains
185character escapes, including some character escapes that part of JSON5
186and which are not found in the canonical RFC 8259 spec.
187Those escapes will need to be translated into standard JSON prior to
188rendering the JSON as text, or into their actual UTF8 characters if this
189element is [json_extract|extracted] into SQL.
190The payload is the UTF8 text representation of the escaped string value.
191The payload does <i>not</i> include string delimiters.
192
193<li value="10"><p><b>TEXTRAW</b> &rarr;
194The element is a JSON string value that contains
195UTF8 characters that need to be escaped if this string is rendered into
196standard JSON text.
197The payload does <i>not</i> include string delimiters.
198
199<li value="11"><p><b>ARRAY</b> &rarr;
200The element is a JSON array. The payload contains
201JSONB elements that comprise values contained within the array.
202
203<li value="12"><p><b>OBJECT</b> &rarr;
204The element is a JSON object. The payload contains
205pairs of JSONB elements that comprise entries for the JSON object.
206The first element in each pair must be a string (types 7 through 10).
207The second element of each pair may be any types, including nested
208arrays or objects.
209
210<li value="13"><p><b>RESERVED-13</b> &rarr;
211Reserved for future expansion. Legacy implements that encounter this
212element type should raise an error.
213
214<li value="14"><p><b>RESERVED-14</b> &rarr;
215Reserved for future expansion. Legacy implements that encounter this
216element type should raise an error.
217
218<li value="15"><p><b>RESERVED-15</b> &rarr;
219Reserved for future expansion. Legacy implements that encounter this
220element type should raise an error.
221</ol>
222
223Element types outside the range of 0 to 12 are reserved for future
224expansion. The current implement raises an error if see an element type
225other than those listed above. However, future versions of SQLite might
226use of the three remaining element types to implement indexing or similar
227optimizations, to speed up lookup against large JSON arrays and/or objects.
228
229### 3.3 Design Rationale For Element Types
230
231A key goal of JSONB is that it should be quick to translate
232to and from text JSON and/or be constructed from SQL values.
233When converting from text into JSONB, we do not want the
234converter subroutine to burn CPU cycles converting elements
235values into some standard format which might never be used.
236Format conversion is "lazy" - it is deferred until actually
237needed. This has implications for the JSONB format design:
238
239 1. Numeric values are stored as text, not a numbers. The values are
240 a direct copy of the text JSON values from which they are derived.
241
242 2. There are multiple element types depending on the details of value
243 formats. For example, INT is used for pure RFC-8259 integer
244 literals and INT5 exists for JSON5 extensions such as hexadecimal
245 notation. FLOAT is used for pure RFC-8259 floating point literals
246 and FLOAT5 is used for JSON5 extensions. There are four different
247 representations of strings, depending on where the string came from
248 and how special characters within the string are escaped.
249
250A second goal of JSONB is that it should be capable of serving as the
251"parse tree" for JSON when a JSON value is being processed by the
252various [JSON SQL functions] built into SQLite. Before JSONB was
253developed, operations such [json_replace()] and [json_patch()]
254and similar worked in three stages:
255
256
257 1. Translate the text JSON into a internal format that is
258 easier to scan and edit.
259 2. Perform the requested operation on the JSON.
260 3. Translate the internal format back into text.
261
262JSONB seeks to serve as the internal format directly - bypassing
263the first and third stages of that process. Since most of the CPU
264cycles are spent on the first and third stages, that suggests that
265JSONB processing will be much faster than text JSON processing.
266
267So when processing JSONB, only the second stage of the three-stage
268process is required. But when processing text JSON, it is still necessary
269to do stages one and three. If JSONB is to be used as the internal
270binary representation, this is yet another reason to store numeric
271values as text. Storing numbers as text minimizes the amount of
272conversion work needed for stages one and three. This is also why
273there are four different representations of text in JSONB. Different
274text representations are used for text coming from different sources
275(RFC-8259 JSON, JSON5, or SQL string values) and conversions only
276happen if and when they are actually needed.
277
278### 3.4 Valid JSONB BLOBs
279
280A valid JSONB BLOB consists of a single JSON element. The element must
281exactly fill the BLOB. This one element is often a JSON object or array
282and those usually contain additional elements as its payload, but the
283element can be a primitive value such a string, number, boolean, or null.
284
285When the built-in JSON functions are attempting to determine if a BLOB
286argument is a JSONB or just a random BLOB, they look at the header of
287the outer element to see that it is well-formed and that the element
288completely fills the BLOB. If these conditions are met, then the BLOB
289is accepted as a JSONB value.