-
Notifications
You must be signed in to change notification settings - Fork 1
/
jsonb_presentation.sql
194 lines (159 loc) · 4.41 KB
/
jsonb_presentation.sql
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
SELECT '{"c":0, "a":2,"a":1,"d":3,"b":5}'::json, '{"c":0, "a":2,"a":1,"d":"3","b":5}'::jsonb;
-- json | jsonb
-- ------------------------------------+------------------------------------
-- {"c":0, "a":2,"a":1,"d":3,"b":5} | {"a": 1, "b": 5, "c": 0, "d": "3"}
-- (1 row)
-- JSON Primitive Types ---
----------------------- jsonb string ---------------------------
select E'\u0001' as value;
-- value
-- -------
-- \x01
-- (1 row)
select E'\u0000' as value;
-- ERROR: invalid Unicode escape value at or near "E'\u0000"
-- LINE 1: select E'\u0000' as value;
SELECT '1234\u0000';
-- ?column?
-- ------------
-- 1234\u0000
-- (1 row)
SELECT E'My star \u2B50';
-- ?column?
-- -----------
-- My star ⭐
-- (1 row)
SELECT E'1234\u0000';
-- ERROR: invalid Unicode escape value at or near "E'1234\u0000"
-- LINE 1: SELECT E'1234\u0000';
SELECT '"1234\u0000"'::jsonb;
-- ERROR: unsupported Unicode escape sequence
-- LINE 1: SELECT '"1234\u0000"'::jsonb;
-- ^
-- DETAIL: \u0000 cannot be converted to text.
-- CONTEXT: JSON data, line 1: ...
SELECT '"My face \u2B50"'::jsonb;
-- jsonb
-- -------------
-- "My face ⭐"
-- (1 row)
----------------------- jsonb number ---------------------------
SELECT jsonb_typeof('1'::jsonb);
-- jsonb_typeof
-- --------------
-- number
-- (1 row)
SELECT jsonb_typeof('-1'::jsonb);
-- jsonb_typeof
-- --------------
-- number
-- (1 row)
-- NaN (not a number) value is used to represent undefined calculational results
select 'infinity'::float / 'infinity'::float;
-- ?column?
-- ----------
-- NaN
-- (1 row)
SELECT jsonb_typeof('inf'::jsonb);
-- ERROR: invalid input syntax for type json
-- LINE 1: SELECT jsonb_typeof('inf'::jsonb);
-- ^
-- DETAIL: Token "inf" is invalid.
-- CONTEXT: JSON data, line 1: inf
SELECT jsonb_typeof('-inf'::jsonb);
-- ERROR: invalid input syntax for type json
-- LINE 1: SELECT jsonb_typeof('-inf'::jsonb);
-- ^
-- DETAIL: Token "-inf" is invalid.
-- CONTEXT: JSON data, line 1: -inf
SELECT jsonb_typeof('NAN'::jsonb);
-- ERROR: invalid input syntax for type json
-- LINE 1: SELECT jsonb_typeof('NAN'::jsonb);
-- ^
-- DETAIL: Token "NAN" is invalid.
-- CONTEXT: JSON data, line 1: NAN
----------------------- jsonb boolean ---------------------------
SELECT 'true'::jsonb;
-- jsonb
-- -------
-- true
-- (1 row)
SELECT jsonb_typeof('true'::jsonb);
-- jsonb_typeof
-- --------------
-- boolean
-- (1 row)
SELECT jsonb_typeof('false'::jsonb);
-- jsonb_typeof
-- --------------
-- boolean
-- (1 row)
SELECT jsonb_typeof('False'::jsonb);
-- ERROR: invalid input syntax for type json
-- LINE 1: SELECT jsonb_typeof('False'::jsonb);
-- ^
-- DETAIL: Token "False" is invalid.
-- CONTEXT: JSON data, line 1: False
SELECT jsonb_typeof('True'::jsonb);
-- ERROR: invalid input syntax for type json
-- LINE 1: SELECT jsonb_typeof('True'::jsonb);
-- ^
-- DETAIL: Token "True" is invalid.
-- CONTEXT: JSON data, line 1: True
SELECT jsonb_typeof('"True"'::jsonb);
-- jsonb_typeof
-- --------------
-- string
-- (1 row)
----------------------- null and Null in jsonb and postgres---------------------------
select '{"a": 1, "b": null}'::jsonb->'c';
-- ?column?
-- ----------
-- (1 row)
select '{"a": 1, "b": null}'::jsonb->'b';
-- ?column?
-- ----------
-- null
-- (1 row)
select '{"a": 1, "b": null}'::jsonb->'b' IS NULL;
-- ?column?
-- ----------
-- f
-- (1 row)
select '{"a": 1, "b": null}'::jsonb->'c' IS NULL;
-- ?column?
-- ----------
-- t
-- (1 row)
select '{"a": 1, "b": null}'::jsonb->>'b' IS Null;
-- ?column?
-- ----------
-- t
-- (1 row)
select '{"a": 1, "b": null}'::jsonb->>'c' IS Null;
-- ?column?
-- ----------
-- t
-- (1 row)
-- explain from PostgreSQL
-- One of the design principles of PostgreSQL, however,
-- is that casting anything to text should give something parsable back to the original value (whenever possible).
select '{"a": 1, "b": null}'::jsonb->>'b';
-- ?column?
-- ----------
-- (1 row)
select '{"a": 1, "b": "null"}'::jsonb->>'b';
-- ?column?
-- ----------
-- null
-- (1 row)
select pg_typeof('{"a": 1, "b": "null"}'::jsonb->>'b');
-- pg_typeof
-- -----------
-- text
-- (1 row)
select pg_typeof('{"a": 1, "b": null}'::jsonb->>'b');
-- pg_typeof
-- -----------
-- text
-- (1 row)