Postgresql provides a data-type jsonb to save data from JSON format. There are two ways for it - using json
and jsonb. This article will clarify difference in short terms.
json and jsonb are very similar to each other. The key difference is - jsonb is binary representation of json -
as per postgresql documentation.
- Pros -
jsonb
- Improved efficiency
- Postgresql provides query interface for these types. So a direct query for any key in column can be made.
- Simple database schema
- Cons -
jsonb
- Slight overhead to convert into binary form.
- Aggregate queries are slower (due to lack of statistics).
- Due to large table footprints may take large disk space.
…
Querying json field
Creating tablecreate table foo (id serial NOT NULL primary key, metadata json)inserting test datainsert into foo(metadata) values ('{ "property": "bar1" }'), ('{ "property": "bar2" }')fetching data from table fooselect * from foo
selecting json fieldselect id, metadata ->'property' as property from foo
Cheers!!!
References -
Swapnil Gourshete
Sidekiq By Example : Ruby's background worker