Debian Codegraph @

www/json1.html

raw
1 <!DOCTYPE html>
2 <html><head>
3 <meta name="viewport" content="width=device-width, initial-scale=1.0">
4 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
5 <link href="sqlite.css" rel="stylesheet">
6 <title>JSON Functions And Operators</title>
7 <!-- path= -->
8 </head>
9 <body>
10 <div class=nosearch>
11 <a href="index.html">
12 <img class="logo" src="images/sqlite370_banner.gif" alt="SQLite" border="0">
13 </a>
14 <div><!-- IE hack to prevent disappearing logo --></div>
15 <div class="tagline desktoponly">
16 Small. Fast. Reliable.<br>Choose any three.
17 </div>
18 <div class="menu mainmenu">
19 <ul>
20 <li><a href="index.html">Home</a>
21 <li class='mobileonly'><a href="javascript:void(0)" onclick='toggle_div("submenu")'>Menu</a>
22 <li class='wideonly'><a href='about.html'>About</a>
23 <li class='desktoponly'><a href="docs.html">Documentation</a>
24 <li class='desktoponly'><a href="download.html">Download</a>
25 <li class='wideonly'><a href='copyright.html'>License</a>
26 <li class='desktoponly'><a href="support.html">Support</a>
27 <li class='desktoponly'><a href="prosupport.html">Purchase</a>
28 <li class='search' id='search_menubutton'>
29 <a href="javascript:void(0)" onclick='toggle_search()'>Search</a>
30 </ul>
31 </div>
32 <div class="menu submenu" id="submenu">
33 <ul>
34 <li><a href='about.html'>About</a>
35 <li><a href='docs.html'>Documentation</a>
36 <li><a href='download.html'>Download</a>
37 <li><a href='support.html'>Support</a>
38 <li><a href='prosupport.html'>Purchase</a>
39 </ul>
40 </div>
41 <div class="searchmenu" id="searchmenu">
42 <form method="GET" action="search">
43 <select name="s" id="searchtype">
44 <option value="d">Search Documentation</option>
45 <option value="c">Search Changelog</option>
46 </select>
47 <input type="text" name="q" id="searchbox" value="">
48 <input type="submit" value="Go">
49 </form>
50 </div>
51 </div>
52 <script>
53 function toggle_div(nm) {
54 var w = document.getElementById(nm);
55 if( w.style.display=="block" ){
56 w.style.display = "none";
57 }else{
58 w.style.display = "block";
59 }
60 }
61 function toggle_search() {
62 var w = document.getElementById("searchmenu");
63 if( w.style.display=="block" ){
64 w.style.display = "none";
65 } else {
66 w.style.display = "block";
67 setTimeout(function(){
68 document.getElementById("searchbox").focus()
69 }, 30);
70 }
71 }
72 function div_off(nm){document.getElementById(nm).style.display="none";}
73 window.onbeforeunload = function(e){div_off("submenu");}
74 /* Disable the Search feature if we are not operating from CGI, since */
75 /* Search is accomplished using CGI and will not work without it. */
76 if( !location.origin || !location.origin.match || !location.origin.match(/http/) ){
77 document.getElementById("search_menubutton").style.display = "none";
78 }
79 /* Used by the Hide/Show button beside syntax diagrams, to toggle the */
80 function hideorshow(btn,obj){
81 var x = document.getElementById(obj);
82 var b = document.getElementById(btn);
83 if( x.style.display!='none' ){
84 x.style.display = 'none';
85 b.innerHTML='show';
86 }else{
87 x.style.display = '';
88 b.innerHTML='hide';
89 }
90 return false;
91 }
92 var antiRobot = 0;
93 function antiRobotGo(){
94 if( antiRobot!=3 ) return;
95 antiRobot = 7;
96 var j = document.getElementById("mtimelink");
97 if(j && j.hasAttribute("data-href")) j.href=j.getAttribute("data-href");
98 }
99 function antiRobotDefense(){
100 document.body.onmousedown=function(){
101 antiRobot |= 2;
102 antiRobotGo();
103 document.body.onmousedown=null;
104 }
105 document.body.onmousemove=function(){
106 antiRobot |= 2;
107 antiRobotGo();
108 document.body.onmousemove=null;
109 }
110 setTimeout(function(){
111 antiRobot |= 1;
112 antiRobotGo();
113 }, 100)
114 antiRobotGo();
115 }
116 antiRobotDefense();
117 </script>
118 <div class=fancy>
119 <div class=nosearch>
120 <div class="fancy_title">
121 JSON Functions And Operators
122 </div>
123 <div class="fancy_toc">
124 <a onclick="toggle_toc()">
125 <span class="fancy_toc_mark" id="toc_mk">&#x25ba;</span>
126 Table Of Contents
127 </a>
128 <div id="toc_sub"><div class="fancy-toc1"><a href="#overview">1. Overview</a></div>
129 <div class="fancy-toc1"><a href="#compiling_in_json_support">2. Compiling in JSON Support</a></div>
130 <div class="fancy-toc1"><a href="#interface_overview">3. Interface Overview</a></div>
131 <div class="fancy-toc2"><a href="#json_arguments">3.1. JSON arguments</a></div>
132 <div class="fancy-toc2"><a href="#jsonb">3.2. JSONB</a></div>
133 <div class="fancy-toc3"><a href="#the_jsonb_format">3.2.1. The JSONB format</a></div>
134 <div class="fancy-toc3"><a href="#handling_of_malformed_jsonb">3.2.2. Handling of malformed JSONB</a></div>
135 <div class="fancy-toc2"><a href="#path_arguments">3.3. PATH arguments</a></div>
136 <div class="fancy-toc2"><a href="#value_arguments">3.4. VALUE arguments</a></div>
137 <div class="fancy-toc2"><a href="#compatibility">3.5. Compatibility</a></div>
138 <div class="fancy-toc2"><a href="#json5_extensions">3.6. JSON5 Extensions</a></div>
139 <div class="fancy-toc2"><a href="#performance_considerations">3.7. Performance Considerations</a></div>
140 <div class="fancy-toc2"><a href="#the_json_blob_input_bug">3.8. The JSON BLOB Input Bug</a></div>
141 <div class="fancy-toc1"><a href="#function_details">4. Function Details</a></div>
142 <div class="fancy-toc2"><a href="#the_json_function">4.1. The json() function</a></div>
143 <div class="fancy-toc2"><a href="#the_jsonb_function">4.2. The jsonb() function</a></div>
144 <div class="fancy-toc2"><a href="#the_json_array_function">4.3. The json_array() function</a></div>
145 <div class="fancy-toc2"><a href="#the_jsonb_array_function">4.4. The jsonb_array() function</a></div>
146 <div class="fancy-toc2"><a href="#the_json_array_length_function">4.5. The json_array_length() function</a></div>
147 <div class="fancy-toc2"><a href="#the_json_error_position_function">4.6. The json_error_position() function</a></div>
148 <div class="fancy-toc2"><a href="#the_json_extract_function">4.7. The json_extract() function</a></div>
149 <div class="fancy-toc2"><a href="#the_jsonb_extract_function">4.8. The jsonb_extract() function</a></div>
150 <div class="fancy-toc2"><a href="#the_and_operators">4.9. The -> and ->> operators</a></div>
151 <div class="fancy-toc2"><a href="#the_json_insert_json_replace_and_json_set_functions">4.10. The json_insert(), json_replace, and json_set() functions</a></div>
152 <div class="fancy-toc2"><a href="#the_jsonb_insert_jsonb_replace_and_jsonb_set_functions">4.11. The jsonb_insert(), jsonb_replace, and jsonb_set() functions</a></div>
153 <div class="fancy-toc2"><a href="#the_json_object_function">4.12. The json_object() function</a></div>
154 <div class="fancy-toc2"><a href="#the_jsonb_object_function">4.13. The jsonb_object() function</a></div>
155 <div class="fancy-toc2"><a href="#the_json_patch_function">4.14. The json_patch() function</a></div>
156 <div class="fancy-toc2"><a href="#the_jsonb_patch_function">4.15. The jsonb_patch() function</a></div>
157 <div class="fancy-toc2"><a href="#the_json_pretty_function">4.16. The json_pretty() function</a></div>
158 <div class="fancy-toc2"><a href="#the_json_remove_function">4.17. The json_remove() function</a></div>
159 <div class="fancy-toc2"><a href="#the_jsonb_remove_function">4.18. The jsonb_remove() function</a></div>
160 <div class="fancy-toc2"><a href="#the_json_type_function">4.19. The json_type() function</a></div>
161 <div class="fancy-toc2"><a href="#the_json_valid_function">4.20. The json_valid() function</a></div>
162 <div class="fancy-toc2"><a href="#the_json_quote_function">4.21. The json_quote() function</a></div>
163 <div class="fancy-toc2"><a href="#array_and_object_aggregate_functions">4.22. Array and object aggregate functions</a></div>
164 <div class="fancy-toc2"><a href="#the_json_each_and_json_tree_table_valued_functions">4.23. The json_each() and json_tree() table-valued functions</a></div>
165 <div class="fancy-toc3"><a href="#examples_using_json_each_and_json_tree_">4.23.1. Examples using json_each() and json_tree()</a></div>
166 </div>
167 </div>
168 <script>
169 function toggle_toc(){
170 var sub = document.getElementById("toc_sub")
171 var mk = document.getElementById("toc_mk")
172 if( sub.style.display!="block" ){
173 sub.style.display = "block";
174 mk.innerHTML = "&#x25bc;";
175 } else {
176 sub.style.display = "none";
177 mk.innerHTML = "&#x25ba;";
178 }
179 }
180 </script>
181 </div>
182
183
184
185
186
187 <h1 id="overview"><span>1. </span>Overview</h1>
188 <p>
189 By default, SQLite supports thirty functions and two operators for
190 dealing with JSON values. There are also two <a href="vtab.html#tabfunc2">table-valued functions</a>
191 that can be used to decompose a JSON string.
192
193 </p><p>
194 There are twenty-six scalar functions and operators:
195
196 </p><ol>
197 <li value='1'>
198 <a href='#jmini'>json</a>(<i>json</i>)
199 </li>
200
201 <li value='2'>
202 <a href='#jminib'>jsonb</a>(<i>json</i>)
203 </li>
204
205 <li value='3'>
206 <a href='#jarray'>json_array</a>(<i>value1</i>,<i>value2</i>,...)
207 </li>
208
209 <li value='4'>
210 <a href='#jarrayb'>jsonb_array</a>(<i>value1</i>,<i>value2</i>,...)
211 </li>
212
213 <li value='5'>
214 <a href='#jarraylen'>json_array_length</a>(<i>json</i>)<br><a href='#jarraylen'>json_array_length</a>(<i>json</i>,<i>path</i>)
215 </li>
216
217 <li value='6'>
218 <a href='#jerr'>json_error_position</a>(<i>json</i>)
219 </li>
220
221 <li value='7'>
222 <a href='#jex'>json_extract</a>(<i>json</i>,<i>path</i>,...)
223 </li>
224
225 <li value='8'>
226 <a href='#jexb'>jsonb_extract</a>(<i>json</i>,<i>path</i>,...)
227 </li>
228
229 <li value='9'>
230 <i>json</i> <a href='#jptr'>-></a> <i>path</i>
231 </li>
232
233 <li value='10'>
234 <i>json</i> <a href='#jptr'>->></a> <i>path</i>
235 </li>
236
237 <li value='11'>
238 <a href='#jins'>json_insert</a>(<i>json</i>,<i>path</i>,<i>value</i>,...)
239 </li>
240
241 <li value='12'>
242 <a href='#jinsb'>jsonb_insert</a>(<i>json</i>,<i>path</i>,<i>value</i>,...)
243 </li>
244
245 <li value='13'>
246 <a href='#jobj'>json_object</a>(<i>label1</i>,<i>value1</i>,...)
247 </li>
248
249 <li value='14'>
250 <a href='#jobjb'>jsonb_object</a>(<i>label1</i>,<i>value1</i>,...)
251 </li>
252
253 <li value='15'>
254 <a href='#jpatch'>json_patch</a>(<i>json</i>1,json2)
255 </li>
256
257 <li value='16'>
258 <a href='#jpatchb'>jsonb_patch</a>(<i>json</i>1,json2)
259 </li>
260
261 <li value='17'>
262 <a href='#jpretty'>json_pretty</a>(<i>json</i>)
263 </li>
264
265 <li value='18'>
266 <a href='#jrm'>json_remove</a>(<i>json</i>,<i>path</i>,...)
267 </li>
268
269 <li value='19'>
270 <a href='#jrmb'>jsonb_remove</a>(<i>json</i>,<i>path</i>,...)
271 </li>
272
273 <li value='20'>
274 <a href='#jrepl'>json_replace</a>(<i>json</i>,<i>path</i>,<i>value</i>,...)
275 </li>
276
277 <li value='21'>
278 <a href='#jreplb'>jsonb_replace</a>(<i>json</i>,<i>path</i>,<i>value</i>,...)
279 </li>
280
281 <li value='22'>
282 <a href='#jset'>json_set</a>(<i>json</i>,<i>path</i>,<i>value</i>,...)
283 </li>
284
285 <li value='23'>
286 <a href='#jsetb'>jsonb_set</a>(<i>json</i>,<i>path</i>,<i>value</i>,...)
287 </li>
288
289 <li value='24'>
290 <a href='#jtype'>json_type</a>(<i>json</i>)<br><a href='#jtype'>json_type</a>(<i>json</i>,<i>path</i>)
291 </li>
292
293 <li value='25'>
294 <a href='#jvalid'>json_valid</a>(<i>json</i>)<br><a href='#jvalid'>json_valid</a>(<i>json</i>,flags)
295 </li>
296
297 <li value='26'>
298 <a href='#jquote'>json_quote</a>(<i>value</i>)
299 </li>
300
301
302 </ol>
303
304 <p>There are four <a href="lang_aggfunc.html">aggregate SQL functions</a>:
305
306 </p><ol>
307 <li value='27'>
308 <a href='#jgrouparray'>json_group_array</a>(<i>value</i>)
309 </li>
310
311 <li value='28'>
312 <a href='#jgrouparrayb'>jsonb_group_array</a>(<i>value</i>)
313 </li>
314
315 <li value='29'>
316 <a href='#jgroupobject'>json_group_object</a>(<i>label</i>,<i>value</i>)
317 </li>
318
319 <li value='30'>
320 <a href='#jgroupobjectb'>jsonb_group_object</a>(name,<i>value</i>)
321 </li>
322
323
324 </ol>
325
326 <p>The two <a href="vtab.html#tabfunc2">table-valued functions</a> are:
327
328 </p><ol>
329 <li value='31'>
330 <a href='#jeach'>json_each</a>(<i>json</i>)<br><a href='#jeach'>json_each</a>(<i>json</i>,<i>path</i>)
331 </li>
332
333 <li value='32'>
334 <a href='#jtree'>json_tree</a>(<i>json</i>)<br><a href='#jtree'>json_tree</a>(<i>json</i>,<i>path</i>)
335 </li>
336
337
338 </ol>
339
340
341
342 <style>
343 .jans {color: #050;}
344 .jex {color: #025;}
345 </style>
346
347
348 <a name="howtocompile"></a>
349
350 <h1 id="compiling_in_json_support"><span>2. </span>Compiling in JSON Support</h1>
351
352 <p>
353 The JSON functions and operators are built into SQLite by default,
354 as of SQLite version 3.38.0 (2022-02-22). They can be omitted
355 by adding the -DSQLITE_OMIT_JSON compile-time option. Prior to
356 version 3.38.0, the JSON functions were an extension that would only
357 be included in builds if the -DSQLITE_ENABLE_JSON1 compile-time option
358 was included. In other words, the JSON functions went from being
359 opt-in with SQLite version 3.37.2 and earlier to opt-out with
360 SQLite version 3.38.0 and later.
361
362 </p><h1 id="interface_overview"><span>3. </span>Interface Overview</h1>
363
364 <p>
365 SQLite stores JSON as ordinary text.
366 Backwards compatibility constraints mean that SQLite is only able to
367 store values that are NULL, integers, floating-point numbers, text,
368 and BLOBs. It is not possible to add a new "JSON" type.
369
370
371 </p><h2 id="json_arguments"><span>3.1. </span>JSON arguments</h2>
372
373 <p>
374 For functions that accept JSON as their first argument, that argument
375 can be a JSON object, array, number, string, or null. SQLite numeric
376 values and NULL values are interpreted as JSON numbers and nulls, respectively.
377 SQLite text values can be understood as JSON objects, arrays, or strings.
378 If an SQLite text value that is not a well-formed JSON object, array, or
379 string is passed into JSON function, that function will usually throw
380 an error. (Exceptions to this rule are <a href="json1.html#jvalid">json_valid()</a>,
381 <a href="json1.html#jquote">json_quote()</a>, and <a href="json1.html#jerr">json_error_position()</a>.)
382
383 </p><p>
384 These routines understand all
385 <a href="https://www.rfc-editor.org/rfc/rfc8259.txt">rfc-8259 JSON syntax</a>
386 and also <a href="https://spec.json5.org/">JSON5 extensions</a>. JSON text
387 generated by these routines always strictly conforms to the
388 <a href="https://json.org">canonical JSON definition</a> and does not contain any JSON5
389 or other extensions. The ability to read and understand JSON5 was added in
390 version 3.42.0 (2023-05-16).
391 Prior versions of SQLite would only read canonical JSON.
392
393 <a name="jsonbx"></a>
394
395 </p><h2 id="jsonb"><span>3.2. </span>JSONB</h2>
396
397 <p>
398 Beginning with version 3.45.0 (2024-01-15), SQLite allows its
399 internal "parse tree" representation of JSON to be stored on disk,
400 as a BLOB, in a format that we call "JSONB". By storing SQLite's internal
401 binary representation of JSON directly in the database, applications
402 can bypass the overhead of parsing and rendering JSON when reading and
403 updating JSON values. The internal JSONB format also uses slightly
404 less disk space then text JSON.
405
406 </p><p>
407 Any SQL function parameter that accepts text JSON as an input will also
408 accept a BLOB in the JSONB format. The function will operate the
409 same in either case, except that it will run faster when
410 the input is JSONB, since it does not need to run the JSON parser.
411
412 </p><p>
413 Most SQL functions that return JSON text have a corresponding function
414 that returns the equivalent JSONB. The functions that return JSON
415 in the text format begin with "<tt>json_</tt>" and functions that
416 return the binary JSONB format begin with "<tt>jsonb_</tt>".
417
418 </p><h3 id="the_jsonb_format"><span>3.2.1. </span>The JSONB format</h3>
419
420 <p>
421 JSONB is a binary representation of JSON used by SQLite and
422 is intended for internal use by SQLite only. Applications
423 should not use JSONB outside of SQLite nor try to reverse-engineer the
424 JSONB format.
425
426 </p><p>
427 The "JSONB" name is inspired by <a href="https://postgresql.org">PostgreSQL</a>, but the
428 on-disk format for SQLite's JSONB is not the same as PostgreSQL's.
429 The two formats have the same name, but are not binary compatible.
430 The PostgreSQL JSONB format claims to offer O(1)
431 lookup of elements in objects and arrays. SQLite's JSONB format makes no
432 such claim. SQLite's JSONB has O(N) time complexity for
433 most operations in SQLite, just like text JSON. The advantage of JSONB in
434 SQLite is that it is smaller and faster than text JSON - potentially several
435 times faster. There is space in the
436 on-disk JSONB format to add enhancements and future versions of SQLite might
437 include options to provide O(1) lookup of elements in JSONB, but no such
438 capability is currently available.
439
440 </p><h3 id="handling_of_malformed_jsonb"><span>3.2.2. </span>Handling of malformed JSONB</h3>
441
442 <p>
443 The JSONB that is generated by SQLite will always be well-formed. If you
444 follow recommended practice and
445 treat JSONB as an opaque BLOB, then you will not have any problems. But
446 JSONB is just a BLOB, so a mischievous programmer could devise BLOBs
447 that are similar to JSONB but that are technically malformed. When
448 misformatted JSONB is feed into JSON functions, any of the following
449 might happen:
450
451 </p><ul>
452 <li><p>
453 The SQL statement might abort with a "malformed JSON" error.
454
455 </p></li><li><p>
456 The correct answer might be returned, if the malformed parts of
457 the JSONB blob do not impact the answer.
458
459 </p></li><li><p>
460 A goofy or nonsensical answer might be returned.
461 </p></li></ul>
462
463 <p>
464 The way in which SQLite handles invalid JSONB might change
465 from one version of SQLite to the next. The system follows
466 the garbage-in/garbage-out rule: If you feed the JSON functions invalid
467 JSONB, you get back an invalid answer. If you are in doubt about the
468 validity of our JSONB, use the <a href="json1.html#jvalid">json_valid()</a> function to verify it.
469
470 </p><p>
471 We do make this one promise:
472 Malformed JSONB will never cause a memory
473 error or similar problem that might lead to a vulnerability.
474 Invalid JSONB might lead to crazy answers,
475 or it might cause queries to abort, but it won't cause a crash.
476
477 <a name="jsonpath"></a>
478
479 </p><h2 id="path_arguments"><span>3.3. </span>PATH arguments</h2>
480
481 <p>
482 For functions that accept PATH arguments, that PATH must be well-formed or
483 else the function will throw an error.
484 A well-formed PATH is a text value that begins with exactly one
485 '$' character followed by zero or more instances
486 of ".<i>objectlabel</i>" or "&#91;<i>arrayindex</i>&#93;".
487
488 </p><p>
489 The <i>arrayindex</i> is usually a non-negative integer <i>N</i>. In
490 that case, the array element selected is the <i>N</i>-th element
491 of the array, starting with zero on the left.
492 The <i>arrayindex</i> can also be of the form "<b>#-</b><i>N</i>"
493 in which case the element selected is the <i>N</i>-th from the
494 right. The last element of the array is "<b>#-1</b>". Think of
495 the "#" characters as the "number of elements in the array". Then
496 the expression "#-1" evaluates to the integer that corresponds to
497 the last entry in the array. It is sometimes useful for the array
498 index to be just the <b>#</b> character, for example when appending
499 a value to an existing JSON array:
500
501 <ul>
502 <li><span class='jex'>json_set('[0,1,2]','$[#]','new')</span>
503 <span class='jans'>&rarr; '[0,1,2,"new"]'</span></li>
504
505 </ul>
506
507
508 <a name="varg"></a>
509
510 </p><h2 id="value_arguments"><span>3.4. </span>VALUE arguments</h2>
511
512 <p>
513 For functions that accept "<i>value</i>" arguments (also shown as
514 "<i>value1</i>" and "<i>value2</i>"),
515 those arguments are usually understood
516 to be literal strings that are quoted and become JSON string values
517 in the result. Even if the input <i>value</i> strings look like
518 well-formed JSON, they are still interpreted as literal strings in the
519 result.
520
521 </p><p>
522 However, if a <i>value</i> argument comes directly from the result of another
523 JSON function or from <a href="json1.html#jptr">the -> operator</a> (but not <a href="json1.html#jptr">the ->> operator</a>),
524 then the argument is understood to be actual JSON and
525 the complete JSON is inserted rather than a quoted string.
526
527 </p><p>
528 For example, in the following call to json_object(), the <i>value</i>
529 argument looks like a well-formed JSON array. However, because it is just
530 ordinary SQL text, it is interpreted as a literal string and added to the
531 result as a quoted string:
532
533 <ul>
534 <li><span class='jex'>json_object('ex','[52,3.14159]')</span>
535 <span class='jans'>&rarr; '{"ex":"[52,3.14159]"}'</span></li>
536
537 <li><span class='jex'>json_object('ex',('[52,3.14159]'->>'$'))</span>
538 <span class='jans'>&rarr; '{"ex":"[52,3.14159]"}'</span></li>
539
540 </ul>
541
542
543 </p><p>
544 But if the <i>value</i> argument in the outer json_object() call is the
545 result of another JSON function like <a href="json1.html#jmini">json()</a> or <a href="json1.html#jarray">json_array()</a>, then
546 the value is understood to be actual JSON and is inserted as such:
547
548 <ul>
549 <li><span class='jex'>json_object('ex',json('[52,3.14159]'))</span>
550 <span class='jans'>&rarr; '{"ex":[52,3.14159]}'</span></li>
551
552 <li><span class='jex'>json_object('ex',json_array(52,3.14159))</span>
553 <span class='jans'>&rarr; '{"ex":[52,3.14159]}'</span></li>
554
555 <li><span class='jex'>json_object('ex','[52,3.14159]'->'$')</span>
556 <span class='jans'>&rarr; '{"ex":[52,3.14159]}'</span></li>
557
558 </ul>
559
560
561 </p><p>
562 To be clear: "<i>json</i>" arguments are always interpreted as JSON
563 regardless of where the value for that argument comes from. But
564 "<i>value</i>" arguments are only interpreted as JSON if those arguments
565 come directly from another JSON function or <a href="json1.html#jptr">the -> operator</a>.
566
567 </p><p>
568 Within JSON value arguments interpreted as JSON strings, Unicode escape
569 sequences are not treated as equivalent to the characters or escaped
570 control characters represented by the expressed Unicode code point.
571 Such escape sequences are not translated or specially treated; they
572 are treated as plain text by SQLite's JSON functions.
573
574 </p><h2 id="compatibility"><span>3.5. </span>Compatibility</h2>
575
576 <p>
577 The current implementation of this JSON library uses a recursive descent
578 parser. In order to avoid using excess stack space, any JSON input that has
579 more than 1000 levels of nesting is considered invalid. Limits on nesting
580 depth are allowed for compatible implementations of JSON by
581 <a href="https://tools.ietf.org/html/rfc8259#section-9">RFC-8259 section 9</a>.
582
583 <a name="json5"></a>
584
585 </p><h2 id="json5_extensions"><span>3.6. </span>JSON5 Extensions</h2>
586
587 <p>
588 Beginning in version 3.42.0 (2023-05-16), these routines will
589 read and interpret input JSON text that includes
590 <a href="https://spec.json5.org/">JSON5</a> extensions. However, JSON text generated
591 by these routines will always be strictly conforming to the
592 <a href="https://json.org">canonical definition of JSON</a>.
593
594 </p><p>
595 Here is a synopsis of JSON5 extensions (adapted from the
596 <a href="https://spec.json5.org/#introduction">JSON5 specification</a>):
597
598 </p><ul>
599 <li> Object keys may be unquoted identifiers.
600 </li><li> Objects may have a single trailing comma.
601 </li><li> Arrays may have a single trailing comma.
602 </li><li> Strings may be single quoted.
603 </li><li> Strings may span multiple lines by escaping new line characters.
604 </li><li> Strings may include new character escapes.
605 </li><li> Numbers may be hexadecimal.
606 </li><li> Numbers may have a leading or trailing decimal point.
607 </li><li> Numbers may be "Infinity", "-Infinity", and "NaN".
608 </li><li> Numbers may begin with an explicit plus sign.
609 </li><li> Single (//...) and multi-line (/*...*/) comments are allowed.
610 </li><li> Additional white space characters are allowed.
611 </li></ul>
612
613 <p>
614 To convert string X from JSON5 into canonical JSON, invoke
615 "<a href="json1.html#jmini">json(X)</a>". The output of the "<a href="json1.html#jmini">json()</a>" function will be canonical
616 JSON regardless of any JSON5 extensions that are present in the input.
617 For backwards compatibility, the <a href="json1.html#jvalid">json_valid(X)</a> function without a
618 "flags" argument continues
619 to report false for inputs that are not canonical JSON, even if the
620 input is JSON5 that the function is able to understand. To determine
621 whether or not an input string is valid JSON5, include the 0x02 bit
622 in the "flags" argument to json_valid: "<tt>json_valid(X,2)</tt>".
623
624 </p><p>
625 These routines understand all of JSON5, plus a little more.
626 SQLite extends the JSON5 syntax in these two ways:
627
628 </p><ol>
629 <li><p>
630 Strict JSON5 requires that
631 unquoted object keys must be ECMAScript 5.1 IdentifierNames. But large
632 unicode tables and lots of code is required in order to determine whether or
633 not a key is an ECMAScript 5.1 IdentifierName. For this reason,
634 SQLite allows object keys to include any unicode characters
635 greater than U+007f that are not whitespace characters. This relaxed
636 definition of "identifier" greatly simplifies the implementation and allows
637 the JSON parser to be smaller and run faster.
638
639 </p></li><li><p>
640 JSON5 allows floating-point infinities to be expressed as
641 "<tt>Infinity</tt>", "<tt>-Infinity</tt>", or "<tt>+Infinity</tt>"
642 in exactly that case - the initial "I" is capitalized and all other
643 characters are lower case. SQLite also allows the abbreviation "<tt>Inf</tt>"
644 to be used in place of "<tt>Infinity</tt>" and it allows both keywords
645 to appear in any combination of upper and lower case letters.
646 Similarly,
647 JSON5 allows "NaN" for not-a-number. SQLite extends this to also allow
648 "QNaN" and "SNaN" in any combination of upper and lower case letters.
649 Note that SQLite interprets NaN, QNaN, and SNaN as just an alternative
650 spellings for "null".
651 This extension has been added because (we are told) there exists a lot
652 of JSON in the wild that includes these non-standard representations
653 for infinity and not-a-number.
654 </p></li></ol>
655
656
657 <h2 id="performance_considerations"><span>3.7. </span>Performance Considerations</h2>
658
659 <p>
660 Most JSON functions do their internal processing using JSONB. So if the
661 input is text, they first most translate the input text into JSONB.
662 If the input is already in the JSONB format, no translation is needed,
663 that step can be skipped, and performance is faster.
664
665 </p><p>
666 For that reason,
667 when an argument to one JSON function is supplied by another
668 JSON function, it is usually more efficient to use the "<tt>jsonb_</tt>"
669 variant for the function used as the argument.
670
671 </p><ul>
672 <li>
673 <tt>... json_insert(A,'$.b',json(C)) ...</tt>
674 &nbsp; &larr; Less efficient.
675 </li><li>
676 <tt>... json_insert(A,'$.b',jsonb(C)) ...</tt>
677 &nbsp; &larr; More efficient.
678 </li></ul>
679
680
681 <p>
682 The <a href="json1.html#jgroupobjectb">aggregate JSON SQL functions</a> are an exception to this rule. Those
683 functions all do their processing using text instead of JSONB. So for the
684 aggregate JSON SQL functions, it is more efficient for the arguments
685 to be supplied using "<tt>json_</tt>" functions than "<tt>jsonb_</tt>"
686 functions.
687
688 </p><ul>
689 <li>
690 <tt>... json_group_array(json(A))) ...</tt>
691 &nbsp; &larr; More efficient.
692 </li><li>
693 <tt>... json_group_array(jsonb(A))) ...</tt>
694 &nbsp; &larr; Less efficient.
695 </li></ul>
696
697 <a name="jblobbug"></a>
698
699 <h2 id="the_json_blob_input_bug"><span>3.8. </span>The JSON BLOB Input Bug</h2>
700
701 <p>If a JSON input is a BLOB that is not JSONB and that looks like
702 text JSON when cast to text, then it is accepted as text JSON.
703 This is actually a long-standing bug in the original implementation
704 that the SQLite developers were unaware of. The documentation stated
705 that a BLOB input to a JSON function should raise an error. But in the
706 actual implementation, the input would be accepted as long
707 as the BLOB content was a valid JSON string in the text encoding of
708 the database.
709
710 </p><p>This JSON BLOB input bug was accidentally fixed when the JSON routines
711 were reimplemented for the 3.45.0 release (2024-01-15).
712 That caused breakage in applications that had come to depend on the old
713 behavior. (In defense of those applications: they were often lured into
714 using BLOBs as JSON by the <a href="cli.html#fileio">readfile()</a> SQL function
715 available in the <a href="cli.html">CLI</a>. Readfile() was used to read JSON from disk files,
716 but readfile() returns a BLOB. And that worked for them, so why not just
717 do it?)
718
719 </p><p>For backwards compatibility,
720 the (formerly incorrect) legacy behavior of interpreting BLOBs as text JSON
721 if no other interpretation works
722 is hereby documented and is be officially supported in
723 version 3.45.1 (2024-01-30) and all subsequent releases.
724
725 </p><h1 id="function_details"><span>4. </span>Function Details</h1>
726
727 <p>The following sections provide additional detail on the operation of
728 the various JSON functions and operators:
729
730 <a name="jmini"></a>
731
732 </p><h2 id="the_json_function"><span>4.1. </span>The json() function</h2>
733
734 <p>The json(X) function verifies that its argument X is a valid
735 JSON string or JSONB blob and returns a minified version of that JSON string
736 with all unnecessary whitespace removed. If X is not a well-formed
737 JSON string or JSONB blob, then this routine throws an error.
738
739 </p><p>If the input is JSON5 text, then it is converted into canonical
740 RFC-8259 text prior to being returned.
741
742 </p><p>If the argument X to json(X) contains JSON objects with duplicate
743 labels, then it is undefined whether or not the duplicates are
744 preserved. The current implementation preserves duplicates.
745 However, future enhancements
746 to this routine may choose to silently remove duplicates.
747
748 </p><p>
749 Example:
750
751 <ul>
752 <li><span class='jex'>json(' { "this" : "is", "a": [ "test" ] } ')</span>
753 <span class='jans'>&rarr; '{"this":"is","a":["test"]}'</span></li>
754
755 </ul>
756
757
758 <a name="jminib"></a>
759
760 </p><h2 id="the_jsonb_function"><span>4.2. </span>The jsonb() function</h2>
761
762 <p>The jsonb(X) function returns the binary JSONB representation
763 of the JSON provided as argument X. An error is raised if X is
764 TEXT that does not have valid JSON syntax.
765
766 </p><p>If X is a BLOB and appears to be JSONB,
767 then this routine simply returns a copy of X.
768 Only the outer-most element of the JSONB input is examined, however.
769 The deep structure of the JSONB is not validated.
770
771 <a name="jarray"></a>
772
773 </p><h2 id="the_json_array_function"><span>4.3. </span>The json_array() function</h2>
774
775 <p>The json_array() SQL function accepts zero or more arguments and
776 returns a well-formed JSON array that is composed from those arguments.
777 If any argument to json_array() is a BLOB then an error is thrown.
778
779 </p><p>An argument with SQL type TEXT is normally converted into a quoted
780 JSON string. However, if the argument is the output from another json1
781 function, then it is stored as JSON. This allows calls to json_array()
782 and <a href="json1.html#jobj">json_object()</a> to be nested. The <a href="json1.html#jmini">json()</a> function can also
783 be used to force strings to be recognized as JSON.
784
785 </p><p>Examples:
786
787 <ul>
788 <li><span class='jex'>json_array(1,2,'3',4)</span>
789 <span class='jans'>&rarr; '[1,2,"3",4]'</span></li>
790
791 <li><span class='jex'>json_array('[1,2]')</span>
792 <span class='jans'>&rarr; '["[1,2]"]'</span></li>
793
794 <li><span class='jex'>json_array(json_array(1,2))</span>
795 <span class='jans'>&rarr; '[[1,2]]'</span></li>
796
797 <li><span class='jex'>json_array(1,null,'3','[4,5]','{"six":7.7}')</span>
798 <span class='jans'>&rarr; '[1,null,"3","[4,5]","{\"six\":7.7}"]'</span></li>
799
800 <li><span class='jex'>json_array(1,null,'3',json('[4,5]'),json('{"six":7.7}'))</span>
801 <span class='jans'>&rarr; '[1,null,"3",[4,5],{"six":7.7}]'</span></li>
802
803 </ul>
804
805
806
807 <a name="jarrayb"></a>
808
809 </p><h2 id="the_jsonb_array_function"><span>4.4. </span>The jsonb_array() function</h2>
810
811 <p>The jsonb_array() SQL function works just like the <a href="json1.html#jarray">json_array()</a>
812 function except that it returns the constructed JSON array in the
813 SQLite's private JSONB format rather than in the standard
814 RFC 8259 text format.
815
816 <a name="jarraylen"></a>
817
818 </p><h2 id="the_json_array_length_function"><span>4.5. </span>The json_array_length() function</h2>
819
820 <p>The json_array_length(X) function returns the number of elements
821 in the JSON array X, or 0 if X is some kind of JSON value other
822 than an array. The json_array_length(X,P) locates the array at path P
823 within X and returns the length of that array, or 0 if path P locates
824 an element in X that is not a JSON array, and NULL if path P does not
825 locate any element of X. Errors are thrown if either X is not
826 well-formed JSON or if P is not a well-formed path.
827
828 </p><p>Examples:
829
830 <ul>
831 <li><span class='jex'>json_array_length('[1,2,3,4]')</span>
832 <span class='jans'>&rarr; 4</span></li>
833
834 <li><span class='jex'>json_array_length('[1,2,3,4]', '$')</span>
835 <span class='jans'>&rarr; 4</span></li>
836
837 <li><span class='jex'>json_array_length('[1,2,3,4]', '$[2]')</span>
838 <span class='jans'>&rarr; 0</span></li>
839
840 <li><span class='jex'>json_array_length('{"one":[1,2,3]}')</span>
841 <span class='jans'>&rarr; 0</span></li>
842
843 <li><span class='jex'>json_array_length('{"one":[1,2,3]}', '$.one')</span>
844 <span class='jans'>&rarr; 3</span></li>
845
846 <li><span class='jex'>json_array_length('{"one":[1,2,3]}', '$.two')</span>
847 <span class='jans'>&rarr; NULL</span></li>
848
849 </ul>
850
851
852
853 <a name="jerr"></a>
854
855 </p><h2 id="the_json_error_position_function"><span>4.6. </span>The json_error_position() function</h2>
856
857 <p>The json_error_position(X) function returns 0 if the input X is a
858 well-formed JSON or JSON5 string. If the input X contains one or more
859 syntax errors, then this function returns the character position of the
860 first syntax error. The left-most character is position 1.
861
862 </p><p>If the input X is a BLOB, then this routine returns 0 if X is
863 a well-formed JSONB blob. If the return value is positive, then it
864 represents the <i>approximate</i> 1-based position in the BLOB of the
865 first detected error.
866
867 </p><p>
868 The json_error_position() function was added with
869 SQLite version 3.42.0 (2023-05-16).
870
871
872 <a name="jex"></a>
873
874 </p><h2 id="the_json_extract_function"><span>4.7. </span>The json_extract() function</h2>
875
876 <p>The json_extract(X,P1,P2,...) extracts and returns one or more
877 values from the
878 well-formed JSON at X. If only a single path P1 is provided, then the
879 SQL datatype of the result is NULL for a JSON null, INTEGER or REAL
880 for a JSON numeric value, an INTEGER zero for a JSON false value,
881 an INTEGER one for a JSON true value, the dequoted text for a
882 JSON string value, and a text representation for JSON object and array values.
883 If there are multiple path arguments (P1, P2, and so forth) then this
884 routine returns SQLite text which is a well-formed JSON array holding
885 the various values.
886
887 </p><p>Examples:
888
889 <ul>
890 <li><span class='jex'>json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$')</span>
891 <span class='jans'>&rarr; '{"a":2,"c":[4,5,{"f":7}]}'</span></li>
892
893 <li><span class='jex'>json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.c')</span>
894 <span class='jans'>&rarr; '[4,5,{"f":7}]'</span></li>
895
896 <li><span class='jex'>json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.c[2]')</span>
897 <span class='jans'>&rarr; '{"f":7}'</span></li>
898
899 <li><span class='jex'>json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.c[2].f')</span>
900 <span class='jans'>&rarr; 7</span></li>
901
902 <li><span class='jex'>json_extract('{"a":2,"c":[4,5],"f":7}','$.c','$.a')</span>
903 <span class='jans'>&rarr; '[[4,5],2]'</span></li>
904
905 <li><span class='jex'>json_extract('{"a":2,"c":[4,5],"f":7}','$.c[#-1]')</span>
906 <span class='jans'>&rarr; 5</span></li>
907
908 <li><span class='jex'>json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.x')</span>
909 <span class='jans'>&rarr; NULL</span></li>
910
911 <li><span class='jex'>json_extract('{"a":2,"c":[4,5,{"f":7}]}', '$.x', '$.a')</span>
912 <span class='jans'>&rarr; '[null,2]'</span></li>
913
914 <li><span class='jex'>json_extract('{"a":"xyz"}', '$.a')</span>
915 <span class='jans'>&rarr; 'xyz'</span></li>
916
917 <li><span class='jex'>json_extract('{"a":null}', '$.a')</span>
918 <span class='jans'>&rarr; NULL</span></li>
919
920 </ul>
921
922
923 </p><p>There is a subtle incompatibility between the json_extract() function
924 in SQLite and the json_extract() function in MySQL. The MySQL version
925 of json_extract() always returns JSON. The SQLite version of
926 json_extract() only returns JSON if there are two or more PATH arguments
927 (because the result is then a JSON array) or if the single PATH argument
928 references an array or object. In SQLite, if json_extract() has only
929 a single PATH argument and that PATH references a JSON null or a string
930 or a numeric value, then json_extract() returns the corresponding SQL
931 NULL, TEXT, INTEGER, or REAL value.
932
933 </p><p>The difference between MySQL json_extract() and SQLite json_extract()
934 really only stands out when accessing individual values within the JSON
935 that are strings or NULLs. The following table demonstrates the difference:
936
937 </p><center>
938 <table border="1" cellpadding="3" cellspacing="0">
939 <tr><th>Operation</th><th>SQLite Result</th><th>MySQL Result
940 </th></tr><tr><td>json_extract('{"a":null,"b":"xyz"}','$.a')</td><td>NULL</td><td>'null'
941 </td></tr><tr><td>json_extract('{"a":null,"b":"xyz"}','$.b')</td><td>'xyz'</td><td>'"xyz"'
942 </td></tr></table></center>
943
944 <a name="jexb"></a>
945
946 <h2 id="the_jsonb_extract_function"><span>4.8. </span>The jsonb_extract() function</h2>
947
948 <p>
949 The jsonb_extract() function works the same as the <a href="json1.html#jex">json_extract()</a> function,
950 except in cases where json_extract() would normally return a text
951 JSON array object, this routine returns the array or object in the
952 JSONB format. For the common case where a text, numeric, null, or
953 boolean JSON element is returned, this routine works exactly the same
954 as json_extract().
955
956
957 <a name="jptr"></a>
958
959 </p><h2 id="the_and_operators"><span>4.9. </span>The -> and ->> operators</h2>
960
961 <p>Beginning with SQLite version 3.38.0 (2022-02-22), the ->
962 and ->> operators are available for extracting subcomponents of JSON.
963 The SQLite implementation of -> and ->> strives to be
964 compatible with both MySQL and PostgreSQL.
965 The -> and ->> operators take a JSON string or JSONB blob
966 as their left operand and a PATH expression or object field
967 label or array index as their right operand. The -> operator
968 returns a text JSON representation of the selected subcomponent or
969 NULL if that subcomponent does not exist. The ->> operator returns
970 an SQL TEXT, INTEGER, REAL, or NULL value that represents the selected
971 subcomponent, or NULL if the subcomponent does not exist.
972
973 </p><p>Both the -> and ->> operators select the same subcomponent
974 of the JSON to their left. The difference is that -> always returns a
975 JSON representation of that subcomponent and the ->> operator always
976 returns an SQL representation of that subcomponent. Thus, these operators
977 are subtly different from a two-argument <a href="json1.html#jex">json_extract()</a> function call.
978 A call to json_extract() with two arguments will return a JSON representation
979 of the subcomponent if and only if the subcomponent is a JSON array or
980 object, and will return an SQL representation of the subcomponent if the
981 subcomponent is a JSON null, string, or numeric value.
982
983 </p><p>When the -> operator returns JSON, it always returns the
984 RFC 8565 text representation of that JSON, not JSONB. Use the
985 <a href="json1.html#jexb">jsonb_extract()</a> function if you need a subcomponent in the
986 JSONB format.
987
988 </p><p>The right-hand operand to the -> and ->> operators can
989 be a well-formed JSON path expression. This is the form used by MySQL.
990 For compatibility with PostgreSQL,
991 the -> and ->> operators also accept a text object label or
992 integer array index as their right-hand operand.
993 If the right operand is a text
994 label X, then it is interpreted as the JSON path '$.X'. If the right
995 operand is an integer value N, then it is interpreted as the JSON path '$[N]'.
996
997 </p><p>Examples:
998
999 <ul>
1000 <li><span class='jex'>'{"a":2,"c":[4,5,{"f":7}]}' -> '$'</span>
1001 <span class='jans'>&rarr; '{"a":2,"c":[4,5,{"f":7}]}'</span></li>
1002
1003 <li><span class='jex'>'{"a":2,"c":[4,5,{"f":7}]}' -> '$.c'</span>
1004 <span class='jans'>&rarr; '[4,5,{"f":7}]'</span></li>
1005
1006 <li><span class='jex'>'{"a":2,"c":[4,5,{"f":7}]}' -> 'c'</span>
1007 <span class='jans'>&rarr; '[4,5,{"f":7}]'</span></li>
1008
1009 <li><span class='jex'>'{"a":2,"c":[4,5,{"f":7}]}' -> '$.c[2]'</span>
1010 <span class='jans'>&rarr; '{"f":7}'</span></li>
1011
1012 <li><span class='jex'>'{"a":2,"c":[4,5,{"f":7}]}' -> '$.c[2].f'</span>
1013 <span class='jans'>&rarr; '7'</span></li>
1014
1015 <li><span class='jex'>'{"a":2,"c":[4,5,{"f":7}]}' ->> '$.c[2].f'</span>
1016 <span class='jans'>&rarr; 7</span></li>
1017
1018 <li><span class='jex'>'{"a":2,"c":[4,5,{"f":7}]}' -> 'c' -> 2 ->> 'f'</span>
1019 <span class='jans'>&rarr; 7</span></li>
1020
1021 <li><span class='jex'>'{"a":2,"c":[4,5],"f":7}' -> '$.c[#-1]'</span>
1022 <span class='jans'>&rarr; '5'</span></li>
1023
1024 <li><span class='jex'>'{"a":2,"c":[4,5,{"f":7}]}' -> '$.x'</span>
1025 <span class='jans'>&rarr; NULL</span></li>
1026
1027 <li><span class='jex'>'[11,22,33,44]' -> 3</span>
1028 <span class='jans'>&rarr; '44'</span></li>
1029
1030 <li><span class='jex'>'[11,22,33,44]' ->> 3</span>
1031 <span class='jans'>&rarr; 44</span></li>
1032
1033 <li><span class='jex'>'{"a":"xyz"}' -> '$.a'</span>
1034 <span class='jans'>&rarr; '"xyz"'</span></li>
1035
1036 <li><span class='jex'>'{"a":"xyz"}' ->> '$.a'</span>
1037 <span class='jans'>&rarr; 'xyz'</span></li>
1038
1039 <li><span class='jex'>'{"a":null}' -> '$.a'</span>
1040 <span class='jans'>&rarr; 'null'</span></li>
1041
1042 <li><span class='jex'>'{"a":null}' ->> '$.a'</span>
1043 <span class='jans'>&rarr; NULL</span></li>
1044
1045 </ul>
1046
1047
1048 <a name="jins"></a>
1049
1050 <a name="jrepl"></a>
1051
1052 <a name="jset"></a>
1053
1054 </p><h2 id="the_json_insert_json_replace_and_json_set_functions"><span>4.10. </span>The json_insert(), json_replace, and json_set() functions</h2>
1055
1056 <p>The json_insert(), json_replace, and json_set() functions all take
1057 a single JSON value as their first argument followed by zero or more
1058 pairs of path and value arguments, and return a new JSON string formed
1059 by updating the input JSON by the path/value pairs. The functions
1060 differ only in how they deal with creating new values and overwriting
1061 preexisting values.
1062
1063 </p><center>
1064 <table border="1" cellpadding="3" cellspacing="0">
1065 <tr>
1066 <th>Function</th><th>Overwrite if already exists?</th><th>Create if does not exist?
1067 </th></tr><tr>
1068 <td>json_insert()</td><td align="center">No</td><td align="center">Yes
1069 </td></tr><tr>
1070 <td>json_replace()</td><td align="center">Yes</td><td align="center">No
1071 </td></tr><tr>
1072 <td>json_set()</td><td align="center">Yes</td><td align="center">Yes
1073 </td></tr></table></center>
1074
1075 <p>The json_insert(), json_replace(), and json_set() functions always
1076 take an odd number of arguments. The first argument is always the original
1077 JSON to be edited. Subsequent arguments occur in pairs with the first
1078 element of each pair being a path and the second element being the value
1079 to insert or replace or set on that path.
1080
1081 </p><p>Edits occur sequentially from left to right. Changes caused by
1082 prior edits can affect the path search for subsequent edits.
1083
1084 </p><p>If the value of a path/value pair is an SQLite TEXT value, then it
1085 is normally inserted as a quoted JSON string, even if the string looks
1086 like valid JSON. However, if the value is the result of another
1087 json function (such as <a href="json1.html#jmini">json()</a> or <a href="json1.html#jarray">json_array()</a> or <a href="json1.html#jobj">json_object()</a>)
1088 or if it is the result of <a href="json1.html#jptr">the -> operator</a>,
1089 then it is interpreted as JSON and is inserted as JSON retaining all
1090 of its substructure. Values that are the result of <a href="json1.html#jptr">the ->> operator</a>
1091 are always interpreted as TEXT and are inserted as a JSON string even
1092 if they look like valid JSON.
1093
1094 </p><p>These routines throw an error if the first JSON argument is not
1095 well-formed or if any PATH argument is not well-formed or if any
1096 argument is a BLOB.
1097
1098 </p><p>To append an element onto the end of an array, using json_insert()
1099 with an array index of "#". Examples:
1100
1101 <ul>
1102 <li><span class='jex'>json_insert('[1,2,3,4]','$[#]',99)</span>
1103 <span class='jans'>&rarr; '[1,2,3,4,99]'</span></li>
1104
1105 <li><span class='jex'>json_insert('[1,[2,3],4]','$[1][#]',99)</span>
1106 <span class='jans'>&rarr; '[1,[2,3,99],4]'</span></li>
1107
1108 </ul>
1109
1110
1111 </p><p>Other examples:
1112
1113 <ul>
1114 <li><span class='jex'>json_insert('{"a":2,"c":4}', '$.a', 99)</span>
1115 <span class='jans'>&rarr; '{"a":2,"c":4}'</span></li>
1116
1117 <li><span class='jex'>json_insert('{"a":2,"c":4}', '$.e', 99)</span>
1118 <span class='jans'>&rarr; '{"a":2,"c":4,"e":99}'</span></li>
1119
1120 <li><span class='jex'>json_replace('{"a":2,"c":4}', '$.a', 99)</span>
1121 <span class='jans'>&rarr; '{"a":99,"c":4}'</span></li>
1122
1123 <li><span class='jex'>json_replace('{"a":2,"c":4}', '$.e', 99)</span>
1124 <span class='jans'>&rarr; '{"a":2,"c":4}'</span></li>
1125
1126 <li><span class='jex'>json_set('{"a":2,"c":4}', '$.a', 99)</span>
1127 <span class='jans'>&rarr; '{"a":99,"c":4}'</span></li>
1128
1129 <li><span class='jex'>json_set('{"a":2,"c":4}', '$.e', 99)</span>
1130 <span class='jans'>&rarr; '{"a":2,"c":4,"e":99}'</span></li>
1131
1132 <li><span class='jex'>json_set('{"a":2,"c":4}', '$.c', '[97,96]')</span>
1133 <span class='jans'>&rarr; '{"a":2,"c":"[97,96]"}'</span></li>
1134
1135 <li><span class='jex'>json_set('{"a":2,"c":4}', '$.c', json('[97,96]'))</span>
1136 <span class='jans'>&rarr; '{"a":2,"c":[97,96]}'</span></li>
1137
1138 <li><span class='jex'>json_set('{"a":2,"c":4}', '$.c', json_array(97,96))</span>
1139 <span class='jans'>&rarr; '{"a":2,"c":[97,96]}'</span></li>
1140
1141 </ul>
1142
1143
1144 <a name="jinsb"></a>
1145
1146 <a name="jreplb"></a>
1147
1148 <a name="jsetb"></a>
1149
1150 </p><h2 id="the_jsonb_insert_jsonb_replace_and_jsonb_set_functions"><span>4.11. </span>The jsonb_insert(), jsonb_replace, and jsonb_set() functions</h2>
1151
1152 <p>The jsonb_insert(), jsonb_replace(), and jsonb_set() functions work the
1153 same as <a href="json1.html#jins">json_insert()</a>, <a href="json1.html#jrepl">json_replace()</a>, and <a href="json1.html#jset">json_set()</a>, respectively,
1154 except that "<tt>jsonb_</tt>" versions return their result in the binary
1155 JSONB format.
1156
1157 <a name="jobj"></a>
1158
1159 </p><h2 id="the_json_object_function"><span>4.12. </span>The json_object() function</h2>
1160
1161 <p>The json_object() SQL function accepts zero or more pairs of arguments
1162 and returns a well-formed JSON object that is composed from those arguments.
1163 The first argument of each pair is the label and the second argument of
1164 each pair is the value.
1165 If any argument to json_object() is a BLOB then an error is thrown.
1166
1167 </p><p>The json_object() function currently allows duplicate labels without
1168 complaint, though this might change in a future enhancement.
1169
1170 </p><p>An argument with SQL type TEXT it is normally converted into a quoted
1171 JSON string even if the input text is well-formed JSON.
1172 However, if the argument is the direct result from another JSON
1173 function or <a href="json1.html#jptr">the -> operator</a> (but not <a href="json1.html#jptr">the ->> operator</a>),
1174 then it is treated as JSON and all of its JSON type information
1175 and substructure is preserved. This allows calls to json_object()
1176 and <a href="json1.html#jarray">json_array()</a> to be nested. The <a href="json1.html#jmini">json()</a> function can also
1177 be used to force strings to be recognized as JSON.
1178
1179 </p><p>Examples:
1180
1181 <ul>
1182 <li><span class='jex'>json_object('a',2,'c',4)</span>
1183 <span class='jans'>&rarr; '{"a":2,"c":4}'</span></li>
1184
1185 <li><span class='jex'>json_object('a',2,'c','{e:5}')</span>
1186 <span class='jans'>&rarr; '{"a":2,"c":"{e:5}"}'</span></li>
1187
1188 <li><span class='jex'>json_object('a',2,'c',json_object('e',5))</span>
1189 <span class='jans'>&rarr; '{"a":2,"c":{"e":5}}'</span></li>
1190
1191 </ul>
1192
1193
1194 <a name="jobjb"></a>
1195
1196 </p><h2 id="the_jsonb_object_function"><span>4.13. </span>The jsonb_object() function</h2>
1197
1198 <p>
1199 The jsonb_object() function works just like the <a href="json1.html#jobj">json_object()</a> function
1200 except that the generated object is returned in the binary JSONB format.
1201
1202 <a name="jpatch"></a>
1203
1204 </p><h2 id="the_json_patch_function"><span>4.14. </span>The json_patch() function</h2>
1205
1206 <p>The json_patch(T,P) SQL function runs the
1207 <a href="https://tools.ietf.org/html/rfc7396">RFC-7396</a> MergePatch algorithm
1208 to apply patch P against input T. The patched copy of T is returned.
1209
1210 </p><p>MergePatch can add, modify, or delete elements of a JSON Object,
1211 and so for JSON Objects, the json_patch() routine is a generalized
1212 replacement for <a href="json1.html#jset">json_set()</a> and <a href="json1.html#jrm">json_remove()</a>. However, MergePatch
1213 treats JSON Array objects as atomic. MergePatch cannot append to an
1214 Array nor modify individual elements of an Array. It can only insert,
1215 replace, or delete the whole Array as a single unit. Hence, json_patch()
1216 is not as useful when dealing with JSON that includes Arrays,
1217 especially Arrays with lots of substructure.
1218
1219 </p><p>Examples:
1220
1221 <ul>
1222 <li><span class='jex'>json_patch('{"a":1,"b":2}','{"c":3,"d":4}')</span>
1223 <span class='jans'>&rarr; '{"a":1,"b":2,"c":3,"d":4}'</span></li>
1224
1225 <li><span class='jex'>json_patch('{"a":[1,2],"b":2}','{"a":9}')</span>
1226 <span class='jans'>&rarr; '{"a":9,"b":2}'</span></li>
1227
1228 <li><span class='jex'>json_patch('{"a":[1,2],"b":2}','{"a":null}')</span>
1229 <span class='jans'>&rarr; '{"b":2}'</span></li>
1230
1231 <li><span class='jex'>json_patch('{"a":1,"b":2}','{"a":9,"b":null,"c":8}')</span>
1232 <span class='jans'>&rarr; '{"a":9,"c":8}'</span></li>
1233
1234 <li><span class='jex'>json_patch('{"a":{"x":1,"y":2},"b":3}','{"a":{"y":9},"c":8}')</span>
1235 <span class='jans'>&rarr; '{"a":{"x":1,"y":9},"b":3,"c":8}'</span></li>
1236
1237 </ul>
1238
1239
1240
1241 <a name="jpatchb"></a>
1242
1243 </p><h2 id="the_jsonb_patch_function"><span>4.15. </span>The jsonb_patch() function</h2>
1244
1245 <p>
1246 The jsonb_patch() function works just like the <a href="json1.html#jpatch">json_patch()</a> function
1247 except that the patched JSON is returned in the binary JSONB format.
1248
1249 <a name="jpretty"></a>
1250
1251 </p><h2 id="the_json_pretty_function"><span>4.16. </span>The json_pretty() function</h2>
1252 <p>
1253 The json_pretty() function works like <a href="json1.html#jmini">json()</a> except that it adds
1254 extra whitespace to make the JSON result easier for humans to read.
1255 The first argument is the JSON or JSONB that is to be pretty-printed.
1256 The optional second argument is a text string that is used for indentation.
1257 If the second argument is omitted or is NULL, then indentation is four
1258 spaces per level.
1259 </p><p>
1260 The json_pretty() function was added with SQLite version 3.46.0
1261 (2024-05-23).
1262
1263 <a name="jrm"></a>
1264
1265 </p><h2 id="the_json_remove_function"><span>4.17. </span>The json_remove() function</h2>
1266
1267 <p>The json_remove(X,P,...) function takes a single JSON value as its
1268 first argument followed by zero or more path arguments.
1269 The json_remove(X,P,...) function returns
1270 a copy of the X parameter with all the elements
1271 identified by path arguments removed. Paths that select elements
1272 not found in X are silently ignored.
1273
1274 </p><p>Removals occurs sequentially from left to right. Changes caused by
1275 prior removals can affect the path search for subsequent arguments.
1276
1277 </p><p>If the json_remove(X) function is called with no path arguments,
1278 then it returns the input X reformatted, with excess whitespace
1279 removed.
1280
1281 </p><p>The json_remove() function throws an error if the first argument
1282 is not well-formed JSON or if any later argument is not a well-formed
1283 path.
1284
1285 </p><p>Examples:
1286
1287 <ul>
1288 <li><span class='jex'>json_remove('[0,1,2,3,4]','$[2]')</span>
1289 <span class='jans'>&rarr; '[0,1,3,4]'</span></li>
1290
1291 <li><span class='jex'>json_remove('[0,1,2,3,4]','$[2]','$[0]')</span>
1292 <span class='jans'>&rarr; '[1,3,4]'</span></li>
1293
1294 <li><span class='jex'>json_remove('[0,1,2,3,4]','$[0]','$[2]')</span>
1295 <span class='jans'>&rarr; '[1,2,4]'</span></li>
1296
1297 <li><span class='jex'>json_remove('[0,1,2,3,4]','$[#-1]','$[0]')</span>
1298 <span class='jans'>&rarr; '[1,2,3]'</span></li>
1299
1300 <li><span class='jex'>json_remove('{"x":25,"y":42}')</span>
1301 <span class='jans'>&rarr; '{"x":25,"y":42}'</span></li>
1302
1303 <li><span class='jex'>json_remove('{"x":25,"y":42}','$.z')</span>
1304 <span class='jans'>&rarr; '{"x":25,"y":42}'</span></li>
1305
1306 <li><span class='jex'>json_remove('{"x":25,"y":42}','$.y')</span>
1307 <span class='jans'>&rarr; '{"x":25}'</span></li>
1308
1309 <li><span class='jex'>json_remove('{"x":25,"y":42}','$')</span>
1310 <span class='jans'>&rarr; NULL</span></li>
1311
1312 </ul>
1313
1314
1315 <a name="jrmb"></a>
1316
1317 </p><h2 id="the_jsonb_remove_function"><span>4.18. </span>The jsonb_remove() function</h2>
1318 <p>
1319 The jsonb_remove() function works just like the <a href="json1.html#jrm">json_remove()</a> function
1320 except that the edited JSON result is returned in the binary JSONB format.
1321
1322 <a name="jtype"></a>
1323
1324 </p><h2 id="the_json_type_function"><span>4.19. </span>The json_type() function</h2>
1325
1326 <p>The json_type(X) function returns the "type" of the outermost element
1327 of X. The json_type(X,P) function returns the "type" of the element
1328 in X that is selected by path P. The "type" returned by json_type() is
1329 one of the following SQL text values:
1330 'null', 'true', 'false', 'integer', 'real', 'text', 'array', or 'object'.
1331 If the path P in json_type(X,P) selects an element that does not exist
1332 in X, then this function returns NULL.
1333
1334 </p><p>The json_type() function throws an error if its first argument is
1335 not well-formed JSON or JSONB or if its second argument is not a well-formed
1336 JSON path.
1337
1338 </p><p>Examples:
1339
1340 <ul>
1341 <li><span class='jex'>json_type('{"a":[2,3.5,true,false,null,"x"]}')</span>
1342 <span class='jans'>&rarr; 'object'</span></li>
1343
1344 <li><span class='jex'>json_type('{"a":[2,3.5,true,false,null,"x"]}','$')</span>
1345 <span class='jans'>&rarr; 'object'</span></li>
1346
1347 <li><span class='jex'>json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a')</span>
1348 <span class='jans'>&rarr; 'array'</span></li>
1349
1350 <li><span class='jex'>json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[0]')</span>
1351 <span class='jans'>&rarr; 'integer'</span></li>
1352
1353 <li><span class='jex'>json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[1]')</span>
1354 <span class='jans'>&rarr; 'real'</span></li>
1355
1356 <li><span class='jex'>json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[2]')</span>
1357 <span class='jans'>&rarr; 'true'</span></li>
1358
1359 <li><span class='jex'>json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[3]')</span>
1360 <span class='jans'>&rarr; 'false'</span></li>
1361
1362 <li><span class='jex'>json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[4]')</span>
1363 <span class='jans'>&rarr; 'null'</span></li>
1364
1365 <li><span class='jex'>json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[5]')</span>
1366 <span class='jans'>&rarr; 'text'</span></li>
1367
1368 <li><span class='jex'>json_type('{"a":[2,3.5,true,false,null,"x"]}','$.a[6]')</span>
1369 <span class='jans'>&rarr; NULL</span></li>
1370
1371 </ul>
1372
1373
1374 <a name="jvalid"></a>
1375
1376 </p><h2 id="the_json_valid_function"><span>4.20. </span>The json_valid() function</h2>
1377
1378 <p>The json_valid(X,Y) function return 1 if the argument X is well-formed
1379 JSON, or returns 0 if X is not well-formed. The Y parameter is an integer
1380 bitmask that defines what is meant by "well-formed". The following bits
1381 of Y are currently defined:
1382
1383 </p><ul>
1384 <li> <b>0x01</b> &rarr;
1385 The input is text that strictly complies with canonical RFC-8259 JSON,
1386 without any extensions.
1387 </li><li> <b>0x02</b> &rarr;
1388 The input is text that is JSON with <a href="json1.html#json5">JSON5</a> extensions described above.
1389 </li><li> <b>0x04</b> &rarr;
1390 The input is a BLOB that superficially appears to be <a href="json1.html#jsonbx">JSONB</a>.
1391 </li><li> <b>0x08</b> &rarr;
1392 The input is a BLOB that strictly conforms to the internal <a href="json1.html#jsonbx">JSONB</a> format.
1393 </li></ul>
1394
1395 <p>By combining bits, the following useful values of Y can be derived:
1396
1397 </p><ul>
1398 <li> <b>1</b> &rarr; X is RFC-8259 JSON text
1399 </li><li> <b>2</b> &rarr; X is <a href="json1.html#json5">JSON5</a> text
1400 </li><li> <b>4</b> &rarr; X is probably <a href="json1.html#jsonbx">JSONB</a>
1401 </li><li> <b>5</b> &rarr; X is RFC-8259 JSON text or <a href="json1.html#jsonbx">JSONB</a>
1402 </li><li> <b>6</b> &rarr; X is <a href="json1.html#json5">JSON5</a> text or <a href="json1.html#jsonbx">JSONB</a>
1403 &larr; <i>This is probably the value you want</i>
1404 </li><li> <b>8</b> &rarr; X is strictly conforming <a href="json1.html#jsonbx">JSONB</a>
1405 </li><li> <b>9</b> &rarr; X is RFC-8259 or strictly conforming <a href="json1.html#jsonbx">JSONB</a>
1406 </li><li> <b>10</b> &rarr; X is JSON5 or strictly conforming <a href="json1.html#jsonbx">JSONB</a>
1407 </li></ul>
1408
1409 <p>The Y parameter is optional. If omitted, it defaults to 1, which means
1410 that the default behavior is to return true only if the input X is
1411 strictly conforming RFC-8259 JSON text without any extensions. This
1412 makes the one-argument version of json_valid() compatible with older
1413 versions of SQLite, prior to the addition of support for
1414 <a href="json1.html#json5">JSON5</a> and <a href="json1.html#jsonbx">JSONB</a>.
1415
1416 </p><p>The difference between 0x04 and 0x08 bits in the Y parameter is that
1417 0x04 only examines the outer wrapper of the BLOB to see if it superficially
1418 looks like <a href="json1.html#jsonbx">JSONB</a>. This is sufficient for must purposes and is very fast.
1419 The 0x08 bit does a thorough examination of all internal details of the BLOB.
1420 The 0x08 bit takes time that is linear in the size of the X input and is much
1421 slower. The 0x04 bit is recommended for most purposes.
1422
1423 </p><p>If you just want to know if a value is a plausible input to one of
1424 the other JSON functions, a Y value of 6 is probably what you want to use.
1425
1426 </p><p>Any Y value less than 1 or greater than 15 raises an error, for the
1427 latest version of json_valid(). However, future versions of json_valid()
1428 might be enhanced to accept flag values outside of this range, having new
1429 meanings that we have not yet thought of.
1430
1431 </p><p>If either X or Y inputs to json_valid() are NULL, then the function
1432 returns NULL.
1433
1434 </p><p>Examples:
1435
1436 <ul>
1437 <li><span class='jex'>json_valid('{"x":35}')</span>
1438 <span class='jans'>&rarr; 1</span></li>
1439
1440 <li><span class='jex'>json_valid('{x:35}')</span>
1441 <span class='jans'>&rarr; 0</span></li>
1442
1443 <li><span class='jex'>json_valid('{x:35}',6)</span>
1444 <span class='jans'>&rarr; 1</span></li>
1445
1446 <li><span class='jex'>json_valid('{"x":35')</span>
1447 <span class='jans'>&rarr; 0</span></li>
1448
1449 <li><span class='jex'>json_valid(NULL)</span>
1450 <span class='jans'>&rarr; NULL</span></li>
1451
1452 </ul>
1453
1454
1455 <a name="jquote"></a>
1456
1457 </p><h2 id="the_json_quote_function"><span>4.21. </span>The json_quote() function</h2>
1458
1459 <p>The json_quote(X) function converts the SQL value X (a number or a
1460 string) into its corresponding JSON representation. If X is a JSON value
1461 returned by another JSON function, then this function is a no-op.
1462
1463 </p><p>Examples:
1464
1465 <ul>
1466 <li><span class='jex'>json_quote(3.14159)</span>
1467 <span class='jans'>&rarr; 3.14159</span></li>
1468
1469 <li><span class='jex'>json_quote('verdant')</span>
1470 <span class='jans'>&rarr; '"verdant"'</span></li>
1471
1472 <li><span class='jex'>json_quote('[1]')</span>
1473 <span class='jans'>&rarr; '"[1]"'</span></li>
1474
1475 <li><span class='jex'>json_quote(json('[1]'))</span>
1476 <span class='jans'>&rarr; '[1]'</span></li>
1477
1478 <li><span class='jex'>json_quote('[1,')</span>
1479 <span class='jans'>&rarr; '"[1,"'</span></li>
1480
1481 </ul>
1482
1483
1484 <a name="jgrouparray"></a>
1485 <a name="jgroupobject"></a>
1486 <a name="jgrouparrayb"></a>
1487 <a name="jgroupobjectb"></a>
1488
1489 </p><h2 id="array_and_object_aggregate_functions"><span>4.22. </span>Array and object aggregate functions</h2>
1490
1491 <p>The json_group_array(X) function is an
1492 <a href="lang_aggfunc.html">aggregate SQL function</a> that returns a JSON array
1493 comprised of all X values in the aggregation.
1494 Similarly, the json_group_object(NAME,VALUE) function returns a JSON object
1495 comprised of all NAME/VALUE pairs in the aggregation.
1496 The "<tt>jsonb_</tt>" variants are the same except that they return their
1497 result in the binary <a href="json1.html#jsonbx">JSONB</a> format.
1498
1499 <a name="jeach"></a>
1500
1501 <a name="jtree"></a>
1502
1503 </p><h2 id="the_json_each_and_json_tree_table_valued_functions"><span>4.23. </span>The json_each() and json_tree() table-valued functions</h2>
1504
1505 <p>The json_each(X) and json_tree(X) <a href="vtab.html#tabfunc2">table-valued functions</a> walk the
1506 JSON value provided as their first argument and return one row for each
1507 element. The json_each(X) function only walks the immediate children
1508 of the top-level array or object,
1509 or just the top-level element itself if the top-level
1510 element is a primitive value.
1511 The json_tree(X) function recursively walks through the
1512 JSON substructure starting with the top-level element.
1513
1514 </p><p>The json_each(X,P) and json_tree(X,P) functions work just like
1515 their one-argument counterparts except that they treat the element
1516 identified by path P as the top-level element.
1517
1518 </p><p>The schema for the table returned by json_each() and json_tree() is
1519 as follows:
1520
1521 </p><blockquote><pre>
1522 CREATE TABLE json_tree(
1523 key ANY, -- key for current element relative to its parent
1524 value ANY, -- value for the current element
1525 type TEXT, -- 'object','array','string','integer', etc.
1526 atom ANY, -- value for primitive types, null for array & object
1527 id INTEGER, -- integer ID for this element
1528 parent INTEGER, -- integer ID for the parent of this element
1529 fullkey TEXT, -- full path describing the current element
1530 path TEXT, -- path to the container of the current row
1531 json JSON HIDDEN, -- 1st input parameter: the raw JSON
1532 root TEXT HIDDEN -- 2nd input parameter: the PATH at which to start
1533 );
1534 </pre></blockquote>
1535
1536 <p>
1537 The "key" column is the integer array index for elements of a JSON array
1538 and the text label for elements of a JSON object. The key column is
1539 NULL in all other cases.
1540
1541 </p><p>
1542 The "atom" column is the SQL value corresponding to primitive elements -
1543 elements other than JSON arrays and objects. The "atom" column is NULL
1544 for a JSON array or object. The "value" column is the same as the
1545 "atom" column for primitive JSON elements but takes on the text JSON value
1546 for arrays and objects.
1547
1548 </p><p>
1549 The "type" column is an SQL text value taken from ('null', 'true', 'false',
1550 'integer', 'real', 'text', 'array', 'object') according to the type of
1551 the current JSON element.
1552
1553 </p><p>
1554 The "id" column is an integer that identifies a specific JSON element
1555 within the complete JSON string. The "id" integer is an internal housekeeping
1556 number, the computation of which might change in future releases. The
1557 only guarantee is that the "id" column will be different for every row.
1558
1559 </p><p>
1560 The "parent" column is always NULL for json_each().
1561 For json_tree(),
1562 the "parent" column is the "id" integer for the parent of the current
1563 element, or NULL for the top-level JSON element or the element identified
1564 by the root path in the second argument.
1565
1566 </p><p>
1567 The "fullkey" column is a text path that uniquely identifies the current
1568 row element within the original JSON string. The complete key to the
1569 true top-level element is returned even if an alternative starting point
1570 is provided by the "root" argument.
1571
1572 </p><p>
1573 The "path" column is the path to the array or object container that holds
1574 the current row, or the path to the current row in the case where the
1575 iteration starts on a primitive type and thus only provides a single
1576 row of output.
1577
1578 </p><h3 id="examples_using_json_each_and_json_tree_"><span>4.23.1. </span>Examples using json_each() and json_tree()</h3>
1579
1580 <p>Suppose the table "CREATE TABLE user(name,phone)" stores zero or
1581 more phone numbers as a JSON array object in the user.phone field.
1582 To find all users who have any phone number with a 704 area code:
1583
1584 </p><blockquote><pre>
1585 SELECT DISTINCT user.name
1586 FROM user, json_each(user.phone)
1587 WHERE json_each.value LIKE '704-%';
1588 </pre></blockquote>
1589
1590 <p>Now suppose the user.phone field contains plain text if the user
1591 has only a single phone number and a JSON array if the user has multiple
1592 phone numbers. The same question is posed: "Which users have a phone number
1593 in the 704 area code?" But now the json_each() function can only be called
1594 for those users that have two or more phone numbers since json_each()
1595 requires well-formed JSON as its first argument:
1596
1597 </p><blockquote><pre>
1598 SELECT name FROM user WHERE phone LIKE '704-%'
1599 UNION
1600 SELECT user.name
1601 FROM user, json_each(user.phone)
1602 WHERE json_valid(user.phone)
1603 AND json_each.value LIKE '704-%';
1604 </pre></blockquote>
1605
1606 <p>Consider a different database with "CREATE TABLE big(json JSON)".
1607 To see a complete line-by-line decomposition of the data:
1608
1609 </p><blockquote><pre>
1610 SELECT big.rowid, fullkey, value
1611 FROM big, json_tree(big.json)
1612 WHERE json_tree.type NOT IN ('object','array');
1613 </pre></blockquote>
1614
1615 <p>In the previous, the "type NOT IN ('object','array')" term of the
1616 WHERE clause suppresses containers and only lets through leaf elements.
1617 The same effect could be achieved this way:
1618
1619 </p><blockquote><pre>
1620 SELECT big.rowid, fullkey, atom
1621 FROM big, json_tree(big.json)
1622 WHERE atom IS NOT NULL;
1623 </pre></blockquote>
1624
1625 <p>Suppose each entry in the BIG table is a JSON object
1626 with a '$.id' field that is a unique identifier
1627 and a '$.partlist' field that can be a deeply nested object.
1628 You want to find the id of every entry that contains one
1629 or more references to uuid '6fa5181e-5721-11e5-a04e-57f3d7b32808' anywhere
1630 in its '$.partlist'.
1631
1632 </p><blockquote><pre>
1633 SELECT DISTINCT json_extract(big.json,'$.id')
1634 FROM big, json_tree(big.json, '$.partlist')
1635 WHERE json_tree.key='uuid'
1636 AND json_tree.value='6fa5181e-5721-11e5-a04e-57f3d7b32808';
1637 </pre></blockquote>
1638 <p align="center"><small><i>This page last modified on <a href="https://sqlite.org/docsrc/honeypot" id="mtimelink" data-href="https://sqlite.org/docsrc/finfo/pages/json1.in?m=d0ca839304">2024-07-25 15:06:57</a> UTC </small></i></p>
1639