Skip to content
Advertisement

Postgres & Rust R2D2: How to get array_to_json as text/string without the escaped double quotes?

I have the following SQL:

select jsonb_agg(t) 
from ( 
 select username, notifications 
 from useraccountview where username = 'Bobby'
) t;

This gives me following in PSQL:

[{"username":"Bobby","notifications":0}]

When I put a ::text:

select jsonb_agg(t)::text 
from (
  select username, notifications 
  from useraccountview where username = 'Bobby'
) t;

I still get the same:

[{"username":"Bobby","notifications":0}]

However on my rust app’s side using r2d2_postgres, I get an escaped string instead:

"[{"username":"Bobby","notifications":0}]"

Rust code:

let qr = conn.query("select jsonb_agg(t)::text from (select username, notifications from useraccount where username = $1) t",&[&param]).unwrap();
let value: &str = qr[0].get(0);
println!("{:?}",value);

Outputs:

"[{"username":"Bobby","notifications":0}]"

How I can I prevent the escaping of double quotes?

Advertisement

Answer

The quotes are only escaped during printing. They are not escaped in memory. Use println!("{}", value); if you want to print the value without the quotes.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement