从 JSON 子数组中删除对象

huangapple go评论108阅读模式
英文:

Remove objects from json subarray of array

问题

我有一个表:

  1. create table public.config
  2. (
  3. id text not null,
  4. payload json not null,
  5. updated_at timestamp with time zone default now() not null
  6. );

json字段具有以下结构:

  1. {
  2. "QuestsCommon":
  3. [
  4. {
  5. "QuestType": "Standard",
  6. "RotationQuestsList": []
  7. },
  8. {
  9. "QuestType": "Daily", 👈️ 需要从子数组 ("RotationQuestsList") 中删除 "QuestType" == "Daily" 的元素
  10. "RotationQuestsList":
  11. [
  12. {
  13. "QuestId": "1", 👈️ 需要删除数组中 "QuestId" == 1 的元素
  14. "GroupId": "Reusable",
  15. "Enabled": false
  16. },
  17. {
  18. "QuestId": "2",
  19. "GroupId": "Reusable",
  20. "Enabled": false
  21. }
  22. ]
  23. },
  24. {
  25. "QuestType": "Weekly",
  26. "RotationQuestsList": []
  27. },
  28. {
  29. "QuestType": "Challenge",
  30. "RotationQuestsList":
  31. [
  32. {
  33. "QuestId": "Lucky",
  34. "GroupId": "Regular",
  35. "Enabled": true
  36. }
  37. ]
  38. }
  39. ]
  40. }

我想编写一个查询,通过父数组的QuestType和子数组的QuestId(例如,QuestId == 1)删除QuestsCommon数组的子数组 (RotationQuestsList) 中的对象。

我发现类似于Remove nested json object from array in postgresHow to remove object from json array?的答案,但仍然卡住了...

英文:

I have a table:

  1. create table public.config
  2. (
  3. id text not null,
  4. payload json not null,
  5. updated_at timestamp with time zone default now() not null
  6. );

and the json field has the following structure:

  1. {
  2. "QuestsCommon":
  3. [
  4. {
  5. "QuestType": "Standard",
  6. "RotationQuestsList": []
  7. },
  8. {
  9. "QuestType": "Daily", 👈️ need to remove elements from subarray ("RotationQuestsList") where "QuestType" == "Daily"
  10. "RotationQuestsList":
  11. [
  12. {
  13. "QuestId": "1", 👈️ need to remove array's element with "QuestId" == 1
  14. "GroupId": "Reusable",
  15. "Enabled": false
  16. },
  17. {
  18. "QuestId": "2",
  19. "GroupId": "Reusable",
  20. "Enabled": false
  21. }
  22. ]
  23. },
  24. {
  25. "QuestType": "Weekly",
  26. "RotationQuestsList": []
  27. },
  28. {
  29. "QuestType": "Challenge",
  30. "RotationQuestsList":
  31. [
  32. {
  33. "QuestId": "Lucky",
  34. "GroupId": "Regular",
  35. "Enabled": true
  36. }
  37. ]
  38. }
  39. ]
  40. }

I'd to write a query that removes object's from the subarray (RotationQuestsList ) of the QuestsCommon array by QuestType of the parent array and QuestId of the subarray (QuestId == 1, for example).

I found answers like Remove nested json object from array in postgres and How to remove object from json array?, but stuck anyway...

答案1

得分: 1

以下是您提供的代码的翻译部分:

  1. WITH updated_quests_common AS (
  2. SELECT id, json_agg(
  3. CASE WHEN element->>'QuestType' != 'Daily' THEN element
  4. ELSE json_build_object('QuestType', 'Daily', 'RotationQuestsList', (
  5. SELECT json_agg(inner_element) FROM json_array_elements((element->>'RotationQuestsList')::json) AS inner_element
  6. WHERE (inner_element->>'QuestId')::int != 1
  7. ))
  8. END
  9. ) quests_common
  10. FROM config,
  11. json_array_elements((payload->>'QuestsCommon')::json) AS element
  12. GROUP BY id
  13. )
  14. UPDATE config SET payload = (payload::jsonb || jsonb_build_object('QuestsCommon', updated_quests_common.quests_common))::json
  15. FROM updated_quests_common
  16. WHERE config.id = updated_quests_common.id;
英文:
  1. WITH updated_quests_common AS(
  2. SELECT id, json_agg(
  3. CASE WHEN element->>'QuestType' != 'Daily' THEN element
  4. ELSE json_build_object('QuestType', 'Daily', 'RotationQuestsList', (
  5. SELECT json_agg(inner_element) FROM json_array_elements((element->>'RotationQuestsList')::json) AS inner_element
  6. WHERE (inner_element->>'QuestId')::int != 1
  7. )) -- this json_build_object need additional work if you have more keys than you provide
  8. END
  9. ) quests_common
  10. FROM config,
  11. json_array_elements((payload->>'QuestsCommon')::json) AS element
  12. GROUP BY id
  13. )
  14. UPDATE config SET payload = (payload::jsonb || jsonb_build_object('QuestsCommon', updated_quests_common.quests_common))::json
  15. FROM updated_quests_common
  16. WHERE config.id = updated_quests_common.id;

Edit: forgot about second condition (need to remove array's element with "QuestId" == 1), hope someone can simplify this.

huangapple
  • 本文由 发表于 2023年5月18日 13:01:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277863.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定