Cela peut se faire avec les fonctions json natives de PostgreSQL. L'exemple db-fiddle ci-dessous inclut quelques données de test supplémentaires.
Schéma (PostgreSQL v13)
CREATE TABLE my_table ("dest" json);
INSERT INTO my_table ("dest") VALUES
('{"DestinationLists": [{"name": "TV3/TVNZ/CHOICE", "destinations": [183, 165]}]}'),
('{"DestinationLists": [{"name": "SecondTest", "destinations": [103, 105]},{"name": "ThirdTest", "destinations": [3, 5]}]}');
Requête n°1
WITH expanded_data AS (
SELECT dest::text,
json_build_object(
'name', dl->>'name',
'destinations', json_agg(json_build_object('Id', dld::text::int))
) AS dest_list_item
FROM my_table,
json_array_elements(dest->'DestinationLists') dl,
json_array_elements(dl->'destinations') dld
GROUP BY dest::text, dl->>'name'
)
SELECT json_build_object('DestinationLists', json_agg(dest_list_item)) AS new_dest
FROM expanded_data
GROUP BY dest::text;
| new_dest |
|---|
| {"DestinationLists":[{"name":"ThirdTest","destinations":[{"Id":3},{"Id":5}]},{"name":"SecondTest","destinations":[{"Id":103},{"Id":105}]}]} |
| {"DestinationLists":[{"name":"TV3/TVNZ/CHOICE","destinations":[{"Id":183},{"Id":165}]}]} |
Modification 1 — mise à jour
Le code ci-dessous s'utilise comme instruction UPDATE ... FROM. La CTE peut aussi être réécrite en sous-requête.
CREATE TABLE my_table (id bigserial, "dest" jsonb);
INSERT INTO my_table ("dest") VALUES
('{"DestinationLists": [{"name": "TV3/TVNZ/CHOICE", "destinations": [183, 165]}]}'),
('{"DestinationLists": [{"name": "SecondTest", "destinations": [103, 105]},{"name": "ThirdTest", "destinations": [3, 5]}]}');
WITH expanded_data AS (
SELECT id,
json_build_object(
'name', dl->>'name',
'destinations', json_agg(json_build_object('Id', dld::text::int))
) AS dest_list_item
FROM my_table,
jsonb_array_elements(dest->'DestinationLists') dl,
jsonb_array_elements(dl->'destinations') dld
GROUP BY id, dl->>'name'
), new_json AS (
SELECT id, json_build_object('DestinationLists', json_agg(dest_list_item)) AS new_dest
FROM expanded_data
GROUP BY id
)
UPDATE my_table
SET dest = new_json.new_dest
FROM new_json
WHERE my_table.id = new_json.id;
Après
SELECT * FROM my_table;
Modification 2 — cas limites
Cette variante traite les listes de destinations vides, que la mise à jour précédente ignorait. Deux enregistrements de test ont été ajoutés : une liste nommée dont une seule entrée comporte des destinations, et une autre dont aucune n'en comporte.
La mise à jour laisse les enregistrements inchangés lorsque rien n'a changé, en comparant le nombre d'éléments nommés au nombre d'éléments vides. L'enregistrement n°4 illustre ce cas.
CREATE TABLE my_table (id bigserial, "dest" jsonb);
INSERT INTO my_table ("dest") VALUES
('{"DestinationLists": [{"name": "TV3/TVNZ/CHOICE", "destinations": [183, 165]}]}'),
('{"DestinationLists": [{"name": "SecondTest", "destinations": [103, 105]},{"name": "ThirdTest", "destinations": [3, 5]}]}'),
('{"DestinationLists": [{"name": "TVNZ, Mediaworks, Choice", "destinations": []}, {"name": "TVNZ, Discovery", "destinations": [165, 183, 4155]}]}'),
('{"DestinationLists": [{"name": "Fourth Test", "destinations": []}]}');
Requête n°3
WITH expanded_data AS (
SELECT id,
CASE WHEN COUNT(dld)=0 THEN 1 ELSE 0 END AS name_has_empty_list_item,
json_build_object(
'name', dl->>'name',
'destinations', CASE WHEN COUNT(dld)=0 THEN to_json(array[]::json[])
ELSE json_agg(json_build_object('Id', dld::text::int)) END
) AS dest_list_item
FROM my_table,
jsonb_array_elements(dest->'DestinationLists') dl
LEFT JOIN jsonb_array_elements(dl->'destinations') dld ON 1=1
GROUP BY id, dl->>'name'
), new_json AS (
SELECT id,
COUNT(dest_list_item) AS no_list_item,
SUM(name_has_empty_list_item) AS no_empty_list_item,
json_build_object('DestinationLists', json_agg(dest_list_item)) AS new_dest
FROM expanded_data
GROUP BY id
HAVING SUM(name_has_empty_list_item) <> COUNT(dest_list_item)
)
SELECT * FROM new_json;
Requête n°4 — mise à jour
WITH expanded_data AS (
SELECT id,
CASE WHEN COUNT(dld)=0 THEN 1 ELSE 0 END AS name_has_empty_list_item,
json_build_object(
'name', dl->>'name',
'destinations', CASE WHEN COUNT(dld)=0 THEN to_json(array[]::json[])
ELSE json_agg(json_build_object('Id', dld::text::int)) END
) AS dest_list_item
FROM my_table,
jsonb_array_elements(dest->'DestinationLists') dl
LEFT JOIN jsonb_array_elements(dl->'destinations') dld ON 1=1
GROUP BY id, dl->>'name'
), new_json AS (
SELECT id, json_build_object('DestinationLists', json_agg(dest_list_item)) AS new_dest
FROM expanded_data
GROUP BY id
HAVING SUM(name_has_empty_list_item) <> COUNT(dest_list_item)
)
UPDATE my_table
SET dest = new_json.new_dest
FROM new_json
WHERE my_table.id = new_json.id;
Aucun résultat à afficher pour cette instruction.
Après la mise à jour
SELECT * FROM my_table;
| id | dest |
|---|---|
| 4 | {"DestinationLists":[{"name":"Fourth Test","destinations":[]}]} |
| 1 | {"DestinationLists":[{"name":"TV3/TVNZ/CHOICE","destinations":[{"Id":183},{"Id":165}]}]} |
| 2 | {"DestinationLists":[{"name":"SecondTest","destinations":[{"Id":103},{"Id":105}]},{"name":"ThirdTest","destinations":[{"Id":3},{"Id":5}]}]} |
| 3 | {"DestinationLists":[{"name":"TVNZ, Discovery","destinations":[{"Id":165},{"Id":183},{"Id":4155}]},{"name":"TVNZ, Mediaworks, Choice","destinations":[]}]} |
Dites-moi si cela vous convient.