build_sql("insert into proposal_table (proposal_id,created_by)
values (",my_id,",",my_name,")")
Doesn't work
build_sql("insert into proposal_table (proposal_id,created_by)
values (",my_id,",",my_name,",",my_proposal,")")
due to an error
Error in UseMethod("escape") :
no applicable method for 'escape' applied to an object of class "json"
The parameterization approach solved the problem and there was no need for the as.character()
I was getting confused by the two different representations of a json object
{"a": "test", "b": "test_b"} - in the postgres docs
[{"a": "foo1", "b": "bar1"}] - output by jsonlite
It seems that both of these can be stored in a postgres jsonb column and be retrieved.
I had gone down a rabbit hole of creating a json object using the json_object which wasn't working because special characters weren't always being escaped properly
my_id<-1
my_name<-'john'
my_proposal<-tibble(a="test",b="test_b",c="Super long complictated")
a<-gather(my_proposal)%>%
mutate(z=paste0("{",key,",",value,"}"))%>%
pull(z)%>%paste0(collapse=",")
variable_for_json<-paste0("json_object('{",a,"}')")
sql<-"insert into proposals (proposal_id,created_by,proposal) values ("
my_name<-paste0("'",my_name,"'")
after_sql<-")"
final_sql<-paste0(sql,my_id,",",my_name,",",variable_for_json,after_sql)
dbSendQuery(x$con,final_sql)