Created
August 20, 2024 14:17
-
-
Save inchoate/eede866b801baaf62c9d7bb643020bd6 to your computer and use it in GitHub Desktop.
Example showing how to rename a field contained within a JSON column in postgres.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | |
-- This nice little query renames `camp_id` to `organization_id` in the JSON column. | |
-- Note, it converts to and from JSONB to do this. | |
-- | |
UPDATE public.data_camp_embeddings | |
SET metadata_ = jsonb_set( | |
-- Convert JSON to JSONB for easier manipulation, and remove the "camp_id" key | |
metadata_::jsonb - 'camp_id', | |
-- Insert "organization_id" key at the top level with the value from "camp_id" | |
'{organization_id}', | |
-- Extract the value of "camp_id" from the original metadata and convert it to integer | |
to_jsonb((metadata_->>'camp_id')::integer) | |
)::json -- Cast back to json after the update | |
WHERE metadata_::jsonb ? 'camp_id'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment