bala
bala

Reputation: 125

How can i fetch a parameter in jsonb column

I have a table like this

| id         | reciever                                     
| (bigint)   |(jsonb)                                      
------------------------------------------------------
|    1       | {"name":"ABC","email":"[email protected]"}
|    2       | {"name":"DEF","email":"[email protected]"}

How can I fetch name where id = 1

the output will be like this

| id         | name                                     
------------------------------------------------------
|    1       | ABC

Upvotes: 0

Views: 38

Answers (3)

Thom Brown
Thom Brown

Reputation: 2029

You can run the following:

SELECT id, receiver->>'name' AS name
FROM mytable
WHERE id = 1;

The ->> operator gets the text value from JSON data by the given key name.

Upvotes: 0

Haleemur Ali
Haleemur Ali

Reputation: 28233

select id, receiver->'name'
from mytable
where id = 1

Check out other JSON operators and functions on the postgres documentation https://www.postgresql.org/docs/12/functions-json.html

Upvotes: 0

user330315
user330315

Reputation:

Use the ->> operator:

select id, receiver ->> 'name' as name
from the_table
where id = 1;

Upvotes: 1

Related Questions