简体   繁体   中英

PostgreSQL: How to use -> and ->> to search SQL JSON?

The below is the json in my 'details' column in a postgreSQL DB. I am trying to do a login check to return the username which matches the password.

{
  "id":"11a8581b-b56f-426e-92f6-a426ba635b98",
  "firstName":"Ryan",
  "lastName":"Bob",
  "username":"ryan",
  "address":"Flat 7, 8 Clisssld Road, London, N16 9AB",
  "email":"ryan@abc.com",
  "password":"$s0$e0801$M/lNYD/JsVN4FoOjs7BwBA==$+C3+A9lAYPMd1YM0FsSbaIzw0wFito4OSEvSrMM/34k="
}
SELECT details -> 'username' 
FROM users WHERE 
details -> 'password' = '$s0$e0801$M/lNYD/JsVN4FoOjs7BwBA==$+C3+A9lAYPMd1YM0FsSbaIzw0wFito4OSEvSrMM/34k=';

PostgreSQL provides two native operators -> and ->> to help you query JSON data.

  • The operator -> returns JSON object field as JSON.
  • The operator ->> returns JSON object field as text.

I think this link could be really helpful in your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM