In following JSON structure,
[
["a", 921],
["b", ""],
..
...
["c", null],
]
how to count occurrence of
-
blank value ""
-
null value
jsonpostgresql
In following JSON structure,
[
["a", 921],
["b", ""],
..
...
["c", null],
]
how to count occurrence of
blank value ""
null value
Best Answer
You can use the function
json_array_elements
to convert your array into a set of elements. Each element is itself an array, so you can use the->
operator to retrieve the second element. Once you have them, use a standardcount(*)
, with the correspondingFILTER
.You can do it with the following SQL statement (the
WITH
helps you view the "step-by-step" approach):... or the following one:
In both cases, you get:
You can check everything at dbfiddle.
NOTE: If you want to use
jsonb
instead ofjson
, just change the::json
with::jsonb
in the first statement, and call thejsonb_array_elements
instead ofjson_array_elements
.