Postgres saves json data with characters \ "

1

When you save the json in a jsonb column in Postgres, save it in this format

"{friend: [{\" name \ ": \" paolo \ ", \" value \ ": \" warrior \ "}, {\" name \ ": \" (label) \ ", \ "value \": \ "\"}]} "

I am sending this to you from ruby on rails

 @proyecto.update(campos: '{"amigo": ' + ' [{"nombre":"paolo","valor":"guerrero"},{"nombre":"(label)","valor":""}]' + "}")

Why does this happen? because postgres changes the double quotes "with \", and how can I make it in my database to be recorded with this format?

{"amigo": [{"nombre":"paolo","valor":"guerrero"},{"nombre":"(label)","valor":""}]}

Help please.

    
asked by HalleyRios 17.05.2018 в 17:53
source

1 answer

2

This is because you are generating a string , but to save in the jsonb column you must use a hash .

Rails console ( $ rails s ):

campos = {
  "amigo": [
    {nombre:"paolo",valor:"guerrero"},
    {nombre:"(label)",valor:""}
  ]
}
#=> {:amigo=>[{:nombre=>"paolo", :valor=>"guerrero"}, {:nombre=>"(label)", :valor=>""}]}

@proyecto.update(campos: campos)
#=> true

@proyecto.campos
#=> {"amigo"=>[{"nombre"=>"paolo", "valor"=>"guerrero"}, {"nombre"=>"(label)", "valor"=>""}]}

Console of bd ( $ rails db ):

=# select * from tabla;
1 | {"amigo": [{"valor": "guerrero", "nombre": "paolo"}, {"valor": "", "nombre": "(label)"}]}

As you can see, although in the rails console the value is displayed as hash , in the database it is saved as json .

    
answered by 17.05.2018 / 18:14
source