-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathjson.html
331 lines (309 loc) · 38 KB
/
json.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>6.12. JSON Functions and Operators — Presto 0.212 Documentation</title>
<link rel="stylesheet" href="../_static/presto.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '0.212',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="top" title="Presto 0.212 Documentation" href="../index.html" />
<link rel="up" title="6. Functions and Operators" href="../functions.html" />
<link rel="next" title="6.13. Date and Time Functions and Operators" href="datetime.html" />
<link rel="prev" title="6.11. Binary Functions and Operators" href="binary.html" />
</head>
<body role="document">
<div class="header">
<h1 class="heading"><a href="../index.html">
<span>Presto 0.212 Documentation</span></a></h1>
<h2 class="heading"><span>6.12. JSON Functions and Operators</span></h2>
</div>
<div class="topnav">
<p class="nav">
<span class="left">
« <a href="binary.html">6.11. Binary Functions and Operators</a>
</span>
<span class="right">
<a href="datetime.html">6.13. Date and Time Functions and Operators</a> »
</span>
</p>
</div>
<div class="content">
<div class="section" id="json-functions-and-operators">
<h1>6.12. JSON Functions and Operators</h1>
<div class="section" id="cast-to-json">
<h2>Cast to JSON</h2>
<blockquote>
<div><p>Casting from <code class="docutils literal"><span class="pre">BOOLEAN</span></code>, <code class="docutils literal"><span class="pre">TINYINT</span></code>, <code class="docutils literal"><span class="pre">SMALLINT</span></code>, <code class="docutils literal"><span class="pre">INTEGER</span></code>,
<code class="docutils literal"><span class="pre">BIGINT</span></code>, <code class="docutils literal"><span class="pre">REAL</span></code>, <code class="docutils literal"><span class="pre">DOUBLE</span></code> or <code class="docutils literal"><span class="pre">VARCHAR</span></code> is supported.
Casting from <code class="docutils literal"><span class="pre">ARRAY</span></code>, <code class="docutils literal"><span class="pre">MAP</span></code> or <code class="docutils literal"><span class="pre">ROW</span></code> is supported when the element type of
the array is one of the supported types, or when the key type of the map
is <code class="docutils literal"><span class="pre">VARCHAR</span></code> and value type of the map is one of the supported types,
or when every field type of the row is one of the supported types.
Behaviors of the casts are shown with the examples below:</p>
<div class="highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="k">NULL</span> <span class="k">AS</span> <span class="n">JSON</span><span class="p">);</span> <span class="c1">-- NULL</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="mi">1</span> <span class="k">AS</span> <span class="n">JSON</span><span class="p">);</span> <span class="c1">-- JSON '1'</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="mi">9223372036854775807</span> <span class="k">AS</span> <span class="n">JSON</span><span class="p">);</span> <span class="c1">-- JSON '9223372036854775807'</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="s1">'abc'</span> <span class="k">AS</span> <span class="n">JSON</span><span class="p">);</span> <span class="c1">-- JSON '"abc"'</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="k">true</span> <span class="k">AS</span> <span class="n">JSON</span><span class="p">);</span> <span class="c1">-- JSON 'true'</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="mi">1</span><span class="p">.</span><span class="mi">234</span> <span class="k">AS</span> <span class="n">JSON</span><span class="p">);</span> <span class="c1">-- JSON '1.234'</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="nb">ARRAY</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">23</span><span class="p">,</span> <span class="mi">456</span><span class="p">]</span> <span class="k">AS</span> <span class="n">JSON</span><span class="p">);</span> <span class="c1">-- JSON '[1,23,456]'</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="nb">ARRAY</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="k">NULL</span><span class="p">,</span> <span class="mi">456</span><span class="p">]</span> <span class="k">AS</span> <span class="n">JSON</span><span class="p">);</span> <span class="c1">-- JSON '[1,null,456]'</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="nb">ARRAY</span><span class="p">[</span><span class="nb">ARRAY</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">23</span><span class="p">],</span> <span class="nb">ARRAY</span><span class="p">[</span><span class="mi">456</span><span class="p">]]</span> <span class="k">AS</span> <span class="n">JSON</span><span class="p">);</span> <span class="c1">-- JSON '[[1,23],[456]]'</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="k">MAP</span><span class="p">(</span><span class="nb">ARRAY</span><span class="p">[</span><span class="s1">'k1'</span><span class="p">,</span> <span class="s1">'k2'</span><span class="p">,</span> <span class="s1">'k3'</span><span class="p">],</span> <span class="nb">ARRAY</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">23</span><span class="p">,</span> <span class="mi">456</span><span class="p">])</span> <span class="k">AS</span> <span class="n">JSON</span><span class="p">);</span> <span class="c1">-- JSON '{"k1":1,"k2":23,"k3":456}'</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="k">MAP</span><span class="p">(</span><span class="nb">ARRAY</span><span class="p">[</span><span class="s1">'k1'</span><span class="p">,</span> <span class="s1">'k2'</span><span class="p">,</span> <span class="s1">'k3'</span><span class="p">],</span> <span class="nb">ARRAY</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">23</span><span class="p">,</span> <span class="mi">456</span><span class="p">])</span> <span class="k">AS</span> <span class="n">JSON</span><span class="p">);</span> <span class="c1">-- JSON '{"k1":1,"k2":23,"k3":456}'</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="k">CAST</span><span class="p">(</span><span class="k">ROW</span><span class="p">(</span><span class="mi">123</span><span class="p">,</span> <span class="s1">'abc'</span><span class="p">,</span> <span class="k">true</span><span class="p">)</span> <span class="k">AS</span> <span class="k">ROW</span><span class="p">(</span><span class="n">v1</span> <span class="nb">BIGINT</span><span class="p">,</span> <span class="n">v2</span> <span class="nb">VARCHAR</span><span class="p">,</span> <span class="n">v3</span> <span class="nb">BOOLEAN</span><span class="p">))</span> <span class="k">AS</span> <span class="n">JSON</span><span class="p">);</span> <span class="c1">-- JSON '[123,"abc",true]'</span>
</pre></div>
</div>
</div></blockquote>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Casting from NULL to <code class="docutils literal"><span class="pre">JSON</span></code> is not straightforward. Casting
from a standalone <code class="docutils literal"><span class="pre">NULL</span></code> will produce a SQL <code class="docutils literal"><span class="pre">NULL</span></code> instead of
<code class="docutils literal"><span class="pre">JSON</span> <span class="pre">'null'</span></code>. However, when casting from arrays or map containing
<code class="docutils literal"><span class="pre">NULL</span></code>s, the produced <code class="docutils literal"><span class="pre">JSON</span></code> will have <code class="docutils literal"><span class="pre">null</span></code>s in it.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">When casting from <code class="docutils literal"><span class="pre">ROW</span></code> to <code class="docutils literal"><span class="pre">JSON</span></code>, the result is a JSON array rather
than a JSON object. This is because positions are more important than
names for rows in SQL.</p>
</div>
</div>
<div class="section" id="cast-from-json">
<h2>Cast from JSON</h2>
<blockquote>
<div><p>Casting to <code class="docutils literal"><span class="pre">BOOLEAN</span></code>, <code class="docutils literal"><span class="pre">TINYINT</span></code>, <code class="docutils literal"><span class="pre">SMALLINT</span></code>, <code class="docutils literal"><span class="pre">INTEGER</span></code>,
<code class="docutils literal"><span class="pre">BIGINT</span></code>, <code class="docutils literal"><span class="pre">REAL</span></code>, <code class="docutils literal"><span class="pre">DOUBLE</span></code> or <code class="docutils literal"><span class="pre">VARCHAR</span></code> is supported.
Casting to <code class="docutils literal"><span class="pre">ARRAY</span></code> and <code class="docutils literal"><span class="pre">MAP</span></code> is supported when the element type of
the array is one of the supported types, or when the key type of the map
is <code class="docutils literal"><span class="pre">VARCHAR</span></code> and value type of the map is one of the supported types.
Behaviors of the casts are shown with the examples below:</p>
<div class="highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="n">JSON</span> <span class="s1">'null'</span> <span class="k">AS</span> <span class="nb">VARCHAR</span><span class="p">);</span> <span class="c1">-- NULL</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="n">JSON</span> <span class="s1">'1'</span> <span class="k">AS</span> <span class="nb">INTEGER</span><span class="p">);</span> <span class="c1">-- 1</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="n">JSON</span> <span class="s1">'9223372036854775807'</span> <span class="k">AS</span> <span class="nb">BIGINT</span><span class="p">);</span> <span class="c1">-- 9223372036854775807</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="n">JSON</span> <span class="s1">'"abc"'</span> <span class="k">AS</span> <span class="nb">VARCHAR</span><span class="p">);</span> <span class="c1">-- abc</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="n">JSON</span> <span class="s1">'true'</span> <span class="k">AS</span> <span class="nb">BOOLEAN</span><span class="p">);</span> <span class="c1">-- true</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="n">JSON</span> <span class="s1">'1.234'</span> <span class="k">AS</span> <span class="n">DOUBLE</span><span class="p">);</span> <span class="c1">-- 1.234</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="n">JSON</span> <span class="s1">'[1,23,456]'</span> <span class="k">AS</span> <span class="nb">ARRAY</span><span class="p">(</span><span class="nb">INTEGER</span><span class="p">));</span> <span class="c1">-- [1, 23, 456]</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="n">JSON</span> <span class="s1">'[1,null,456]'</span> <span class="k">AS</span> <span class="nb">ARRAY</span><span class="p">(</span><span class="nb">INTEGER</span><span class="p">));</span> <span class="c1">-- [1, NULL, 456]</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="n">JSON</span> <span class="s1">'[[1,23],[456]]'</span> <span class="k">AS</span> <span class="nb">ARRAY</span><span class="p">(</span><span class="nb">ARRAY</span><span class="p">(</span><span class="nb">INTEGER</span><span class="p">)));</span> <span class="c1">-- [[1, 23], [456]]</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="n">JSON</span> <span class="s1">'{"k1":1,"k2":23,"k3":456}'</span> <span class="k">AS</span> <span class="k">MAP</span><span class="p">(</span><span class="nb">VARCHAR</span><span class="p">,</span> <span class="nb">INTEGER</span><span class="p">));</span> <span class="c1">-- {k1=1, k2=23, k3=456}</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="n">JSON</span> <span class="s1">'{"v1":123,"v2":"abc","v3":true}'</span> <span class="k">AS</span> <span class="k">ROW</span><span class="p">(</span><span class="n">v1</span> <span class="nb">BIGINT</span><span class="p">,</span> <span class="n">v2</span> <span class="nb">VARCHAR</span><span class="p">,</span> <span class="n">v3</span> <span class="nb">BOOLEAN</span><span class="p">));</span> <span class="c1">-- {v1=123, v2=abc, v3=true}</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="n">JSON</span> <span class="s1">'[123,"abc",true]'</span> <span class="k">AS</span> <span class="k">ROW</span><span class="p">(</span><span class="n">v1</span> <span class="nb">BIGINT</span><span class="p">,</span> <span class="n">v2</span> <span class="nb">VARCHAR</span><span class="p">,</span> <span class="n">v3</span> <span class="nb">BOOLEAN</span><span class="p">));</span> <span class="c1">-- {value1=123, value2=abc, value3=true}</span>
</pre></div>
</div>
</div></blockquote>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>JSON arrays can have mixed element types and JSON maps can have mixed
value types. This makes it impossible to cast them to SQL arrays and maps in
some cases. To address this, Presto supports partial casting of arrays and maps:</p>
<div class="last highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="n">JSON</span> <span class="s1">'[[1, 23], 456]'</span> <span class="k">AS</span> <span class="nb">ARRAY</span><span class="p">(</span><span class="n">JSON</span><span class="p">));</span> <span class="c1">-- [JSON '[1,23]', JSON '456']</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="n">JSON</span> <span class="s1">'{"k1": [1, 23], "k2": 456}'</span> <span class="k">AS</span> <span class="k">MAP</span><span class="p">(</span><span class="nb">VARCHAR</span><span class="p">,</span> <span class="n">JSON</span><span class="p">));</span> <span class="c1">-- {k1 = JSON '[1,23]', k2 = JSON '456'}</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="n">JSON</span> <span class="s1">'[null]'</span> <span class="k">AS</span> <span class="nb">ARRAY</span><span class="p">(</span><span class="n">JSON</span><span class="p">));</span> <span class="c1">-- [JSON 'null']</span>
</pre></div>
</div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">When casting from <code class="docutils literal"><span class="pre">JSON</span></code> to <code class="docutils literal"><span class="pre">ROW</span></code>, both JSON array and JSON object are supported.</p>
</div>
</div>
<div class="section" id="json-functions">
<h2>JSON Functions</h2>
<dl class="function">
<dt id="is_json_scalar">
<code class="descname">is_json_scalar</code><span class="sig-paren">(</span><em>json</em><span class="sig-paren">)</span> → boolean</dt>
<dd><p>Determine if <code class="docutils literal"><span class="pre">json</span></code> is a scalar (i.e. a JSON number, a JSON string, <code class="docutils literal"><span class="pre">true</span></code>, <code class="docutils literal"><span class="pre">false</span></code> or <code class="docutils literal"><span class="pre">null</span></code>):</p>
<div class="highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="n">is_json_scalar</span><span class="p">(</span><span class="s1">'1'</span><span class="p">);</span> <span class="c1">-- true</span>
<span class="k">SELECT</span> <span class="n">is_json_scalar</span><span class="p">(</span><span class="s1">'[1, 2, 3]'</span><span class="p">);</span> <span class="c1">-- false</span>
</pre></div>
</div>
</dd></dl>
<dl class="function">
<dt id="json_array_contains">
<code class="descname">json_array_contains</code><span class="sig-paren">(</span><em>json</em>, <em>value</em><span class="sig-paren">)</span> → boolean</dt>
<dd><p>Determine if <code class="docutils literal"><span class="pre">value</span></code> exists in <code class="docutils literal"><span class="pre">json</span></code> (a string containing a JSON array):</p>
<div class="highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="n">json_array_contains</span><span class="p">(</span><span class="s1">'[1, 2, 3]'</span><span class="p">,</span> <span class="mi">2</span><span class="p">);</span>
</pre></div>
</div>
</dd></dl>
<dl class="function">
<dt id="json_array_get">
<code class="descname">json_array_get</code><span class="sig-paren">(</span><em>json_array</em>, <em>index</em><span class="sig-paren">)</span> → json</dt>
<dd><div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p>The semantics of this function are broken. If the extracted element
is a string, it will be converted into an invalid <code class="docutils literal"><span class="pre">JSON</span></code> value that
is not properly quoted (the value will not be surrounded by quotes
and any interior quotes will not be escaped).</p>
<p class="last">We recommend against using this function. It cannot be fixed without
impacting existing usages and may be removed in a future release.</p>
</div>
<p>Returns the element at the specified index into the <code class="docutils literal"><span class="pre">json_array</span></code>.
The index is zero-based:</p>
<div class="highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="n">json_array_get</span><span class="p">(</span><span class="s1">'["a", [3, 9], "c"]'</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span> <span class="c1">-- JSON 'a' (invalid JSON)</span>
<span class="k">SELECT</span> <span class="n">json_array_get</span><span class="p">(</span><span class="s1">'["a", [3, 9], "c"]'</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span> <span class="c1">-- JSON '[3,9]'</span>
</pre></div>
</div>
<p>This function also supports negative indexes for fetching element indexed
from the end of an array:</p>
<div class="highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="n">json_array_get</span><span class="p">(</span><span class="s1">'["c", [3, 9], "a"]'</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">);</span> <span class="c1">-- JSON 'a' (invalid JSON)</span>
<span class="k">SELECT</span> <span class="n">json_array_get</span><span class="p">(</span><span class="s1">'["c", [3, 9], "a"]'</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">);</span> <span class="c1">-- JSON '[3,9]'</span>
</pre></div>
</div>
<p>If the element at the specified index doesn’t exist, the function returns null:</p>
<div class="highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="n">json_array_get</span><span class="p">(</span><span class="s1">'[]'</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span> <span class="c1">-- null</span>
<span class="k">SELECT</span> <span class="n">json_array_get</span><span class="p">(</span><span class="s1">'["a", "b", "c"]'</span><span class="p">,</span> <span class="mi">10</span><span class="p">);</span> <span class="c1">-- null</span>
<span class="k">SELECT</span> <span class="n">json_array_get</span><span class="p">(</span><span class="s1">'["c", "b", "a"]'</span><span class="p">,</span> <span class="o">-</span><span class="mi">10</span><span class="p">);</span> <span class="c1">-- null</span>
</pre></div>
</div>
</dd></dl>
<dl class="function">
<dt id="json_array_length">
<code class="descname">json_array_length</code><span class="sig-paren">(</span><em>json</em><span class="sig-paren">)</span> → bigint</dt>
<dd><p>Returns the array length of <code class="docutils literal"><span class="pre">json</span></code> (a string containing a JSON array):</p>
<div class="highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="n">json_array_length</span><span class="p">(</span><span class="s1">'[1, 2, 3]'</span><span class="p">);</span>
</pre></div>
</div>
</dd></dl>
<dl class="function">
<dt id="json_extract">
<code class="descname">json_extract</code><span class="sig-paren">(</span><em>json</em>, <em>json_path</em><span class="sig-paren">)</span> → json</dt>
<dd><p>Evaluates the <a class="reference external" href="http://goessner.net/articles/JsonPath/">JSONPath</a>-like expression <code class="docutils literal"><span class="pre">json_path</span></code> on <code class="docutils literal"><span class="pre">json</span></code>
(a string containing JSON) and returns the result as a JSON string:</p>
<div class="highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="n">json_extract</span><span class="p">(</span><span class="n">json</span><span class="p">,</span> <span class="s1">'$.store.book'</span><span class="p">);</span>
</pre></div>
</div>
</dd></dl>
<dl class="function">
<dt id="json_extract_scalar">
<code class="descname">json_extract_scalar</code><span class="sig-paren">(</span><em>json</em>, <em>json_path</em><span class="sig-paren">)</span> → varchar</dt>
<dd><p>Like <a class="reference internal" href="#json_extract" title="json_extract"><code class="xref py py-func docutils literal"><span class="pre">json_extract()</span></code></a>, but returns the result value as a string (as opposed
to being encoded as JSON). The value referenced by <code class="docutils literal"><span class="pre">json_path</span></code> must be a
scalar (boolean, number or string):</p>
<div class="highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="n">json_extract_scalar</span><span class="p">(</span><span class="s1">'[1, 2, 3]'</span><span class="p">,</span> <span class="s1">'$[2]'</span><span class="p">);</span>
<span class="k">SELECT</span> <span class="n">json_extract_scalar</span><span class="p">(</span><span class="n">json</span><span class="p">,</span> <span class="s1">'$.store.book[0].author'</span><span class="p">);</span>
</pre></div>
</div>
</dd></dl>
<dl class="function">
<dt id="json_format">
<code class="descname">json_format</code><span class="sig-paren">(</span><em>json</em><span class="sig-paren">)</span> → varchar</dt>
<dd><p>Returns the JSON text serialized from the input JSON value.
This is inverse function to <a class="reference internal" href="#json_parse" title="json_parse"><code class="xref py py-func docutils literal"><span class="pre">json_parse()</span></code></a>:</p>
<div class="highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="n">json_format</span><span class="p">(</span><span class="n">JSON</span> <span class="s1">'[1, 2, 3]'</span><span class="p">);</span> <span class="c1">-- '[1,2,3]'</span>
<span class="k">SELECT</span> <span class="n">json_format</span><span class="p">(</span><span class="n">JSON</span> <span class="s1">'"a"'</span><span class="p">);</span> <span class="c1">-- '"a"'</span>
</pre></div>
</div>
</dd></dl>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p><a class="reference internal" href="#json_format" title="json_format"><code class="xref py py-func docutils literal"><span class="pre">json_format()</span></code></a> and <code class="docutils literal"><span class="pre">CAST(json</span> <span class="pre">AS</span> <span class="pre">VARCHAR)</span></code> have completely
different semantics.</p>
<p><a class="reference internal" href="#json_format" title="json_format"><code class="xref py py-func docutils literal"><span class="pre">json_format()</span></code></a> serializes the input JSON value to JSON text conforming to
<span class="target" id="index-0"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc7159.html"><strong>RFC 7159</strong></a>. The JSON value can be a JSON object, a JSON array, a JSON string,
a JSON number, <code class="docutils literal"><span class="pre">true</span></code>, <code class="docutils literal"><span class="pre">false</span></code> or <code class="docutils literal"><span class="pre">null</span></code>:</p>
<div class="highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="n">json_format</span><span class="p">(</span><span class="n">JSON</span> <span class="s1">'{"a": 1, "b": 2}'</span><span class="p">);</span> <span class="c1">-- '{"a":1,"b":2}'</span>
<span class="k">SELECT</span> <span class="n">json_format</span><span class="p">(</span><span class="n">JSON</span> <span class="s1">'[1, 2, 3]'</span><span class="p">);</span> <span class="c1">-- '[1,2,3]'</span>
<span class="k">SELECT</span> <span class="n">json_format</span><span class="p">(</span><span class="n">JSON</span> <span class="s1">'"abc"'</span><span class="p">);</span> <span class="c1">-- '"abc"'</span>
<span class="k">SELECT</span> <span class="n">json_format</span><span class="p">(</span><span class="n">JSON</span> <span class="s1">'42'</span><span class="p">);</span> <span class="c1">-- '42'</span>
<span class="k">SELECT</span> <span class="n">json_format</span><span class="p">(</span><span class="n">JSON</span> <span class="s1">'true'</span><span class="p">);</span> <span class="c1">-- 'true'</span>
<span class="k">SELECT</span> <span class="n">json_format</span><span class="p">(</span><span class="n">JSON</span> <span class="s1">'null'</span><span class="p">);</span> <span class="c1">-- 'null'</span>
</pre></div>
</div>
<p><code class="docutils literal"><span class="pre">CAST(json</span> <span class="pre">AS</span> <span class="pre">VARCHAR)</span></code> casts the JSON value to the corresponding SQL VARCHAR value.
For JSON string, JSON number, <code class="docutils literal"><span class="pre">true</span></code>, <code class="docutils literal"><span class="pre">false</span></code> or <code class="docutils literal"><span class="pre">null</span></code>, the cast
behavior is same as the corresponding SQL type. JSON object and JSON array
cannot be cast to VARCHAR:</p>
<div class="last highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="n">JSON</span> <span class="s1">'{"a": 1, "b": 2}'</span> <span class="k">AS</span> <span class="nb">VARCHAR</span><span class="p">);</span> <span class="c1">-- ERROR!</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="n">JSON</span> <span class="s1">'[1, 2, 3]'</span> <span class="k">AS</span> <span class="nb">VARCHAR</span><span class="p">);</span> <span class="c1">-- ERROR!</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="n">JSON</span> <span class="s1">'"abc"'</span> <span class="k">AS</span> <span class="nb">VARCHAR</span><span class="p">);</span> <span class="c1">-- 'abc'; Note the double quote is gone</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="n">JSON</span> <span class="s1">'42'</span> <span class="k">AS</span> <span class="nb">VARCHAR</span><span class="p">);</span> <span class="c1">-- '42'</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="n">JSON</span> <span class="s1">'true'</span> <span class="k">AS</span> <span class="nb">VARCHAR</span><span class="p">);</span> <span class="c1">-- 'true'</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="n">JSON</span> <span class="s1">'null'</span> <span class="k">AS</span> <span class="nb">VARCHAR</span><span class="p">);</span> <span class="c1">-- NULL</span>
</pre></div>
</div>
</div>
<dl class="function">
<dt id="json_parse">
<code class="descname">json_parse</code><span class="sig-paren">(</span><em>string</em><span class="sig-paren">)</span> → json</dt>
<dd><p>Returns the JSON value deserialized from the input JSON text.
This is inverse function to <a class="reference internal" href="#json_format" title="json_format"><code class="xref py py-func docutils literal"><span class="pre">json_format()</span></code></a>:</p>
<div class="highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="n">json_parse</span><span class="p">(</span><span class="s1">'[1, 2, 3]'</span><span class="p">);</span> <span class="c1">-- JSON '[1,2,3]'</span>
<span class="k">SELECT</span> <span class="n">json_parse</span><span class="p">(</span><span class="s1">'"abc"'</span><span class="p">);</span> <span class="c1">-- JSON '"abc"'</span>
</pre></div>
</div>
</dd></dl>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p><a class="reference internal" href="#json_parse" title="json_parse"><code class="xref py py-func docutils literal"><span class="pre">json_parse()</span></code></a> and <code class="docutils literal"><span class="pre">CAST(string</span> <span class="pre">AS</span> <span class="pre">JSON)</span></code> have completely
different semantics.</p>
<p><a class="reference internal" href="#json_parse" title="json_parse"><code class="xref py py-func docutils literal"><span class="pre">json_parse()</span></code></a> expects a JSON text conforming to <span class="target" id="index-1"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc7159.html"><strong>RFC 7159</strong></a>, and returns
the JSON value deserialized from the JSON text.
The JSON value can be a JSON object, a JSON array, a JSON string, a JSON number,
<code class="docutils literal"><span class="pre">true</span></code>, <code class="docutils literal"><span class="pre">false</span></code> or <code class="docutils literal"><span class="pre">null</span></code>:</p>
<div class="highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="n">json_parse</span><span class="p">(</span><span class="s1">'not_json'</span><span class="p">);</span> <span class="c1">-- ERROR!</span>
<span class="k">SELECT</span> <span class="n">json_parse</span><span class="p">(</span><span class="s1">'["a": 1, "b": 2]'</span><span class="p">);</span> <span class="c1">-- JSON '["a": 1, "b": 2]'</span>
<span class="k">SELECT</span> <span class="n">json_parse</span><span class="p">(</span><span class="s1">'[1, 2, 3]'</span><span class="p">);</span> <span class="c1">-- JSON '[1,2,3]'</span>
<span class="k">SELECT</span> <span class="n">json_parse</span><span class="p">(</span><span class="s1">'"abc"'</span><span class="p">);</span> <span class="c1">-- JSON '"abc"'</span>
<span class="k">SELECT</span> <span class="n">json_parse</span><span class="p">(</span><span class="s1">'42'</span><span class="p">);</span> <span class="c1">-- JSON '42'</span>
<span class="k">SELECT</span> <span class="n">json_parse</span><span class="p">(</span><span class="s1">'true'</span><span class="p">);</span> <span class="c1">-- JSON 'true'</span>
<span class="k">SELECT</span> <span class="n">json_parse</span><span class="p">(</span><span class="s1">'null'</span><span class="p">);</span> <span class="c1">-- JSON 'null'</span>
</pre></div>
</div>
<p><code class="docutils literal"><span class="pre">CAST(string</span> <span class="pre">AS</span> <span class="pre">JSON)</span></code> takes any VARCHAR value as input, and returns
a JSON string with its value set to input string:</p>
<div class="last highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="s1">'not_json'</span> <span class="k">AS</span> <span class="n">JSON</span><span class="p">);</span> <span class="c1">-- JSON '"not_json"'</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="s1">'["a": 1, "b": 2]'</span> <span class="k">AS</span> <span class="n">JSON</span><span class="p">);</span> <span class="c1">-- JSON '"[\"a\": 1, \"b\": 2]"'</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="s1">'[1, 2, 3]'</span> <span class="k">AS</span> <span class="n">JSON</span><span class="p">);</span> <span class="c1">-- JSON '"[1, 2, 3]"'</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="s1">'"abc"'</span> <span class="k">AS</span> <span class="n">JSON</span><span class="p">);</span> <span class="c1">-- JSON '"\"abc\""'</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="s1">'42'</span> <span class="k">AS</span> <span class="n">JSON</span><span class="p">);</span> <span class="c1">-- JSON '"42"'</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="s1">'true'</span> <span class="k">AS</span> <span class="n">JSON</span><span class="p">);</span> <span class="c1">-- JSON '"true"'</span>
<span class="k">SELECT</span> <span class="k">CAST</span><span class="p">(</span><span class="s1">'null'</span> <span class="k">AS</span> <span class="n">JSON</span><span class="p">);</span> <span class="c1">-- JSON '"null"'</span>
</pre></div>
</div>
</div>
<dl class="function">
<dt id="json_size">
<code class="descname">json_size</code><span class="sig-paren">(</span><em>json</em>, <em>json_path</em><span class="sig-paren">)</span> → bigint</dt>
<dd><p>Like <a class="reference internal" href="#json_extract" title="json_extract"><code class="xref py py-func docutils literal"><span class="pre">json_extract()</span></code></a>, but returns the size of the value.
For objects or arrays, the size is the number of members,
and the size of a scalar value is zero:</p>
<div class="highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="n">json_size</span><span class="p">(</span><span class="s1">'{"x": {"a": 1, "b": 2}}'</span><span class="p">,</span> <span class="s1">'$.x'</span><span class="p">);</span> <span class="c1">-- 2</span>
<span class="k">SELECT</span> <span class="n">json_size</span><span class="p">(</span><span class="s1">'{"x": [1, 2, 3]}'</span><span class="p">,</span> <span class="s1">'$.x'</span><span class="p">);</span> <span class="c1">-- 3</span>
<span class="k">SELECT</span> <span class="n">json_size</span><span class="p">(</span><span class="s1">'{"x": {"a": 1, "b": 2}}'</span><span class="p">,</span> <span class="s1">'$.x.a'</span><span class="p">);</span> <span class="c1">-- 0</span>
</pre></div>
</div>
</dd></dl>
</div>
</div>
</div>
<div class="bottomnav">
<p class="nav">
<span class="left">
« <a href="binary.html">6.11. Binary Functions and Operators</a>
</span>
<span class="right">
<a href="datetime.html">6.13. Date and Time Functions and Operators</a> »
</span>
</p>
</div>
<div class="footer" role="contentinfo">
</div>
</body>
</html>