From 60fb75c9632f4e8725517f4ed0cde7612dce7c43 Mon Sep 17 00:00:00 2001 From: "Rico Sta. Cruz" Date: Fri, 22 Sep 2017 16:12:28 +0800 Subject: [PATCH] postgresql-json: new cheatsheet --- _drafts/postgresql-json.md | 41 -------------- postgresql-json.md | 113 +++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 41 deletions(-) delete mode 100644 _drafts/postgresql-json.md create mode 100644 postgresql-json.md diff --git a/_drafts/postgresql-json.md b/_drafts/postgresql-json.md deleted file mode 100644 index c190dd99c..000000000 --- a/_drafts/postgresql-json.md +++ /dev/null @@ -1,41 +0,0 @@ ---- -title: Postgresql JSON -category: Development ---- - -``` -SELECT * FROM users WHERE data->>'name' = 'John' -SELECT data->>'name' AS name FROM users -``` - -| Operator | Example | Description | Returns | -| ---- | ---- | ---- | ---- | -| `->` | `data->2` | Get array element `2` | JSON | -| `->` | `data->'name'` | Get object key `name` | JSON | -| `#>` | `data#>'{a,b}'` | Get keypath `a,b` (eg, `data.a.b`) | JSON | -| ---- | ---- | ---- | ---- | -| `->>` | `data->>2` | Get array element `2` | text | -| `->>` | `data->>'name'` | Get object key `name` | text | -| `#>` | `data#>>'{a,b}'` | Get keypath `a,b` (eg, `data.a.b`) | text | - -`>` returns JSON, `>>` returns text. - -### Boolean operators - -| Operator | Example | Description | -| ---- | ---- | ---- | -| `?` | `data ? 'name'` | Does `data` have key `name`? | -| `?|` | `data ?| array['a','b']` | Does `data` have `a` or `b`? | -| `?&` | `data ?& array['a','b']` | Does `data` have `a` and `b`? | -| `@>` | `'{"a":1,"b":2}'::jsonb @> '{"b":2}'::jsonb` | Does `left` include `right`? | -| `<@` | `'{"a":1}'::jsonb <@ '{"a":1,"b":2}'::jsonb` | Does `right` include `left`? | - -When `?`/`?|`/`?&` works on objects, it checks keys; when it works on arrays, it checks for elements. - -- `'{"a":1}'::jsonb ? 'a'` -- `'["a"]'::jsonb ? 'a'` - -## References - -- -- diff --git a/postgresql-json.md b/postgresql-json.md new file mode 100644 index 000000000..c036aab3d --- /dev/null +++ b/postgresql-json.md @@ -0,0 +1,113 @@ +--- +title: Postgresql JSON +layout: 2017/sheet +prism_languages: [sql] +updated: 2017-09-22 +--- + +## Operators + +### Accessors + +```sql +SELECT * FROM users WHERE data->>'name' = 'John'; +SELECT data->>'name' AS name FROM users; +``` +{: .-setup} + +| Operator | Description | Example | Returns | +| ---- | ---- | ---- | ---- | +| `->` _int_ | Get array element `2` | `data->2` | JSON | +| `->` _text_ | Get object key `name` | `data->'name'` | JSON | +| `#>` _text[]_ | Get keypath `a,b` (eg, `data.a.b`) | `data#>'{a,b}'` | JSON | +| - +| `->>` _int_ | Get array element `2` | `data->>2` | Text | +| `->>` _text_ | Get object key `name` | `data->>'name'` | Text | +| `#>>` _text[]_ | Get keypath `a,b` (eg, `data.a.b`) | `data#>>'{a,b}'` | Text | +{: .-headers.-shortcuts} + +`>` returns JSON, `>>` returns text. + +### Boolean operators + +```sql +SELECT * FROM users WHERE data->tags ? 'admin'; +SELECT data->tags ? 'admin' AS is_admin FROM users; +``` +{: .-setup} + +| Operator | Description | Example | +| ---- | ---- | ---- | +| `?` _str_ | Does `data` have key `name`? | `data ? 'name'` | +| `?|` _text[]_ | Does `data` have `a` or `b`? | `data ?| array['a','b']` | +| `?&` _text[]_ | Does `data` have `a` and `b`? | `data ?& array['a','b']` | +| `@>` _jsonb_ | Does `left` include `right`? | `data @> '{"b":2}'::jsonb` | +| `<@` _jsonb_ | Does `right` include `left`? | `data <@ '{"a":1,"b":2}'::jsonb` | +{: .-headers.-shortcuts.-left-align} + +When `?`/`?|`/`?&` works on objects, it checks keys; when it works on arrays, it checks for elements. + +## Updating + +### Arrays and objects + +```sql +UPDATE users SET tags = tags || array['admin']; +``` +{: .-setup} + +| Operator | Example | Description +| ---- | ---- | ---- +| `||` _json_ | `data || array['a','b']` | Concatenate +| `-` _str_ | `data - 'a'` | Delete a key +| `-` _int_ | `data - 1` | Delete an array item +| `#-` _text[]_ | `data #- '{us,name}'` | Delete a path +{: .-headers.-shortcuts} + +Only available in PostgreSQL 9.5+. + +### jsonb_set + +```sql +UPDATE users SET data = jsonb_set(data, '{name}', '"John"'); +``` + +Only available in PostgreSQL 9.5+. + +## Functions + +#### fn(json) → json + +```sql +jsonb_set(data, '{path}', value) +jsonb_strip_nulls(data) +``` + +#### fn(···) → json + +```sql +to_json("Hello"::text) +array_to_json('{1,2}'::int[]) +``` + +#### Iteration + +```sql +SELECT * from json_each('{"a":1, "b":2}') +SELECT * from json_each_text('{"a":1, "b":2}') +-- key | value +``` + +This is an incomplete list, there's way too many! + +See: [JSON functions](https://www.postgresql.org/docs/9.5/static/functions-json.html) + +## More examples + +- `'{"a":1}'::jsonb ? 'a'` +- `'["a"]'::jsonb ? 'a'` + +## References + +- +-