14 SELECT * FROM users WHERE data->>'name' = 'John';
15 SELECT data->>'name' AS name FROM users;
19 | Operator | Description | Example | Returns |
20 | ---- | ---- | ---- | ---- |
21 | `->` _int_ | Get array element `2` | `data->2` | JSON |
22 | `->` _text_ | Get object key `name` | `data->'name'` | JSON |
23 | `#>` _text[]_ | Get keypath `a,b` (eg, `data.a.b`) | `data#>'{a,b}'` | JSON |
25 | `->>` _int_ | Get array element `2` | `data->>2` | Text |
26 | `->>` _text_ | Get object key `name` | `data->>'name'` | Text |
27 | `#>>` _text[]_ | Get keypath `a,b` (eg, `data.a.b`) | `data#>>'{a,b}'` | Text |
28 {: .-headers.-shortcuts}
30 `>` returns JSON, `>>` returns text.
35 SELECT * FROM users WHERE data->tags ? 'admin';
36 SELECT data->tags ? 'admin' AS is_admin FROM users;
40 | Operator | Description | Example |
41 | ---- | ---- | ---- |
42 | `?` _str_ | Does `data` have key `name`? | `data ? 'name'` |
43 | `?|` _text[]_ | Does `data` have `a` or `b`? | `data ?| array['a','b']` |
44 | `?&` _text[]_ | Does `data` have `a` and `b`? | `data ?& array['a','b']` |
45 | `@>` _jsonb_ | Does `left` include `right`? | `data @> '{"b":2}'::jsonb` |
46 | `<@` _jsonb_ | Does `right` include `left`? | `data <@ '{"a":1,"b":2}'::jsonb` |
47 {: .-headers.-shortcuts.-left-align}
49 When `?`/`?|`/`?&` works on objects, it checks keys; when it works on arrays, it checks for elements.
53 ### Arrays and objects
56 UPDATE users SET tags = tags || array['admin'];
60 | Operator | Example | Description
62 | `||` _json_ | `data || array['a','b']` | Concatenate
63 | `-` _str_ | `data - 'a'` | Delete a key
64 | `-` _int_ | `data - 1` | Delete an array item
65 | `#-` _text[]_ | `data #- '{us,name}'` | Delete a path
66 {: .-headers.-shortcuts}
68 Only available in PostgreSQL 9.5+.
73 UPDATE users SET data = jsonb_set(data, '{name}', '"John"');
76 Only available in PostgreSQL 9.5+.
83 jsonb_set(data, '{path}', value)
84 jsonb_strip_nulls(data)
90 to_json("Hello"::text)
91 array_to_json('{1,2}'::int[])
97 SELECT * from json_each('{"a":1, "b":2}')
98 SELECT * from json_each_text('{"a":1, "b":2}')
102 This is an incomplete list, there's way too many!
104 See: [JSON functions](https://www.postgresql.org/docs/9.5/static/functions-json.html)
108 - `'{"a":1}'::jsonb ? 'a'`
109 - `'["a"]'::jsonb ? 'a'`
113 - <https://www.postgresql.org/docs/9.5/static/functions-json.html>
114 - <https://www.postgresql.org/docs/9.5/static/datatype-json.html>