JSON字符串中的插值变量
我正在使用下面的代码来创建一个具有变量的函数,该函数可更新JSONB列,或者如果不存在,则可以创建它 - 在这篇文章的帮助
下变量在JSON字符串中$ 2和$ 3。有什么建议吗?
CREATE OR REPLACE FUNCTION public.updateoffset(site text, offsetnumber integer, toscrape integer)
RETURNS void
LANGUAGE sql
AS $function$
update settings set "offset" = coalesce("offset", '{}') || '{"$2": {"toscrape":3$}}'
where site = $1;
$function$
I am using the code below to create a function with variables that updates a jsonb column with a json object or create it if it doesn't exist - With help from this post
However I am really having trouble interpolating the variables $2 and $3 in the json string. Any suggestions?
CREATE OR REPLACE FUNCTION public.updateoffset(site text, offsetnumber integer, toscrape integer)
RETURNS void
LANGUAGE sql
AS $function$
update settings set "offset" = coalesce("offset", '{}') || '{"$2": {"toscrape":3$}}'
where site = $1;
$function$
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请勿使用字符串插值来构建JSON值 - 使用相反,特别是
json_build_object
:也可能更简单地使用
json_set
:(但是,它确实将其他属性保留在内在对象中,而不是将其完全替换为对象只有
toscrape
作为密钥)Do not use string interpolation for building JSON values - use JSON functions and operators instead, particularly
json_build_object
:Also it might be simpler to use
json_set
:(which, however, does keep other properties in the inner object, not replacing it completely with an object that has only
toscrape
as a key)使用函数
Use the function format().