I have to modify part of a link in a jsonb column from a postgres database.
The object looks like this:
{
"large": {
"ext": ".jpg",
"url": "https://bucket1.s3.eu-central-1.amazonaws.com/large_ansgar_scheffold_IZZ_78_Ugqsow_unsplash_498662ba12.jpg",
"hash": "large_ansgar_scheffold_IZZ_78_Ugqsow_unsplash_498662ba12",
"mime": "image/jpeg",
"name": "large_ansgar-scheffold-IZZ78Ugqsow-unsplash.jpg",
"path": null,
"size": 85.7,
"width": 1000,
"height": 667
},
"small": {
"ext": ".jpg",
"url": "https://bucket1.s3.eu-central-1.amazonaws.com/small_ansgar_scheffold_IZZ_78_Ugqsow_unsplash_498662ba12.jpg",
"hash": "small_ansgar_scheffold_IZZ_78_Ugqsow_unsplash_498662ba12",
"mime": "image/jpeg",
"name": "small_ansgar-scheffold-IZZ78Ugqsow-unsplash.jpg",
"path": null,
"size": 24.44,
"width": 500,
"height": 333
},
"medium": {
"ext": ".jpg",
"url": "https://bucket1.s3.eu-central-1.amazonaws.com/medium_ansgar_scheffold_IZZ_78_Ugqsow_unsplash_498662ba12.jpg",
"hash": "medium_ansgar_scheffold_IZZ_78_Ugqsow_unsplash_498662ba12",
"mime": "image/jpeg",
"name": "medium_ansgar-scheffold-IZZ78Ugqsow-unsplash.jpg",
"path": null,
"size": 50.9,
"width": 750,
"height": 500
},
"thumbnail": {
"ext": ".jpg",
"url": "https://bucket1.s3.eu-central-1.amazonaws.com/thumbnail_ansgar_scheffold_IZZ_78_Ugqsow_unsplash_498662ba12.jpg",
"hash": "thumbnail_ansgar_scheffold_IZZ_78_Ugqsow_unsplash_498662ba12",
"mime": "image/jpeg",
"name": "thumbnail_ansgar-scheffold-IZZ78Ugqsow-unsplash.jpg",
"path": null,
"size": 6.88,
"width": 234,
"height": 156
}
}
I want to change 'bucket1' from each url into 'bucket2'.
I can change the whole url like this:
UPDATE public.files
SET formats = jsonb_set(formats, '{large,url}', '"new-url"')
But how can I change only part of it? Can I somehow combine jsonb_set with regexp_replace? I thought about converting it to string and back but I am not sure how I should do that.