Jsonb
Extracting values
Subscripting
Since v11, Postgresql supports object_name['key']
and array_name[idx]
syntax to extract values as an alternative to object_name->'key'
and array_name->idx
syntax.
The result of a subscripting expression is always of the jsonb data type.
Mapping between JSON and SQL
SELECT value FROM jsonb_array_elements(arrayA)
/**
SELECT intersection_jsonb_arrays('[{"x": ["mark", 1, 1]}, {"x": ["mark", 1, 3]}]',
'[{"x": ["mark", 1, 1]}, {"x": ["mark", 1, 3]}, {"x": ["mark", 2, 1]},
{"x": ["mark", 2, 3]}, {"x": ["mark", 3, 1]}, {"x": ["mark", 3, 2]}, {"x": ["mark", 3, 3]}]');
*/
CREATE OR REPLACE FUNCTION intersection_jsonb_arrays(arrayA JSONB, arrayB JSONB) RETURNS JSONB
LANGUAGE SQL
AS $sql$
SELECT jsonb_agg(DISTINCT value) FROM (
SELECT value FROM jsonb_array_elements(arrayA)
INTERSECT
SELECT value FROM jsonb_array_elements(arrayB)
);
$sql$;
/**
SELECT union_jsonb_arrays('[{"x": ["mark", 1, 1]}, {"x": ["mark", 1, 3]}]',
'[{"x": ["mark", 1, 1]}, {"x": ["mark", 1, 3]}, {"x": ["mark", 2, 1]},
{"x": ["mark", 2, 3]}, {"x": ["mark", 3, 1]}, {"x": ["mark", 3, 2]}, {"x": ["mark", 3, 3]}]');
*/
CREATE OR REPLACE FUNCTION union_jsonb_arrays(arrayA JSONB, arrayB JSONB) RETURNS JSONB
LANGUAGE SQL
AS $sql$
SELECT jsonb_agg(DISTINCT value) FROM (
SELECT value FROM jsonb_array_elements(arrayA)
UNION
SELECT value FROM jsonb_array_elements(arrayB)
);
$sql$;
/*
SELECT except_jsonb_arrays('["red", "blue", "teal", "green", "yellow", "magenta"]', '["green"]');
*/
CREATE OR REPLACE FUNCTION except_jsonb_arrays(arrayA JSONB, arrayB JSONB) RETURNS JSONB
LANGUAGE SQL
AS $sql$
SELECT jsonb_agg(DISTINCT value) FROM (
SELECT value FROM jsonb_array_elements(arrayA)
EXCEPT
SELECT value FROM jsonb_array_elements(arrayB)
);
$sql$;```
### `SELECT key, value FROM jsonb_each(objectA)`
A bug I encountered in a game was the rules were giving more than one "reserves" value to a given player, so I needed to count the number of these arrays within
state arrays to figure out which moves were causing the bug.
```json
[["control","black"],
["reserves","black",1],["reserves","white",3],
["cell","a",3,[]],["cell","a",4,[]],["cell","a",5,[]],["cell","a",6,[]],
["cell","b",2,[]],["cell","b",3,[]],["cell","b",4,[]],["cell","b",5,[]],["cell","b",6,["white"]],["cell","b",7,["black"]],
["cell","c",1,[]],["cell","c",2,["black","white","white","white"]],["cell","c",3,[]],["cell","c",4,["black","white"]],
["cell","c",5,["black"]],["cell","c",6,[]],["cell","c",7,["black"]],["cell","c",8,[]],
["cell","d",1,[]],["cell","d",2,[]],["cell","d",3,[]],["cell","d",4,[]],
["cell","d",5,[]],["cell","d",6,["black","white","white","black","white"]],["cell","d",7,[]],["cell","d",8,[]],
["cell","e",1,[]],["cell","e",2,["black"]],["cell","e",3,[]],["cell","e",4,[]],
["cell","e",5,[]],["cell","e",6,[]],["cell","e",7,["white"]],["cell","e",8,[]],
["cell","f",1,[]],["cell","f",2,[]],["cell","f",3,["white","white","black"]],["cell","f",4,[]],
["cell","f",5,["black"]],["cell","f",6,[]],["cell","f",7,["black"]],["cell","f",8,[]],
["cell","g",2,["white"]],["cell","g",3,[]],["cell","g",4,["black","white"]],
["cell","g",5,[]],["cell","g",6,["black","white"]],["cell","g",7,["black"]],
["cell","h",3,[]],["cell","h",4,[]],["cell","h",5,[]],["cell","h",6,[]]]