Option A is to use a lot of subqueries or json path queries:
SELECT
t."id",
jsonb_path_query_first(t."additionalFields", 'strict $[*] ?(@.name == "Total Cases") .value') AS "Total Cases",
jsonb_path_query_first(t."additionalFields", 'strict $[*] ?(@.name == "Open Cases") .value') AS "Open Cases",
jsonb_path_query_first(t."additionalFields", 'strict $[*] ?(@.name == "Prod Cases") .value') AS "Prod Cases",
jsonb_path_query_first(t."additionalFields", 'strict $[*] ?(@.name == "Sev1 Cases") .value') AS "Sev1 Cases",
jsonb_path_query_first(t."additionalFields", 'strict $[*] ?(@.name == "Sev2 Cases") .value') AS "Sev2 Cases",
jsonb_path_query_first(t."additionalFields", 'strict $[*] ?(@.name == " Sev1/2 Cases") .value') AS " Sev1/2 Cases",
jsonb_path_query_first(t."additionalFields", 'strict $[*] ?(@.name == "Preview Cases") .value') AS "Preview Cases"
FROM "tasks" t
WHERE t."additionalFields" @> '[{"name":"Open Cases"}]'
To get the result columns not a jsonb
but as other types, see How to convert a json value to text
, to int
, to float
and to boolean
respectively.
Option B is to use a subquery to transform that array of key-value pairs into a JSON object (much more useful also for storage), then use jsonb_to_record
with the required record columns:
SELECT t."id", fields.*
FROM "tasks" t,
LATERAL jsonb_to_record(
SELECT jsonb_object_agg(value->>'name', value->'value')
FROM jsonb_array_elements(t."additionalFields")
) AS fields("Total Cases" jsonb, "Open Cases" jsonb, "Prod Cases" jsonb, "Sev1 Cases" jsonb, "Sev2 Cases" jsonb, " Sev1/2 Cases" jsonb, "Preview Cases" jsonb)
WHERE t."additionalFields" @> '[{"name":"Open Cases"}]';
To get the result columns not a jsonb
, simply declare them as the respective desired primitive type.