# Getting started with JSONB in Postgres

DevFeed: [Getting started with JSONB in Postgres](<https://devfeed.tech/articles/getting-started-with-jsonb-in-postgres-41194.md>)

Original publisher: [Read original article](<https://www.craigkerstiens.com/2017/03/12/getting-started-with-jsonb-in-postgres/>)

Author: Map

Published: 2017-03-12T20:55:56Z

Content type: tutorial

Language: en

Sources: [Craig Kerstiens](<https://devfeed.tech/sources/craig-kerstiens.md>)

Topics: [PostgreSQL](<https://devfeed.tech/topics/postgresql.md>), [JSON](<https://devfeed.tech/topics/json.md>), [Database](<https://devfeed.tech/topics/database.md>), [Query (disambiguation)](<https://devfeed.tech/topics/query.md>)

Tags: [api](<https://devfeed.tech/tags/api.md>), [database](<https://devfeed.tech/tags/database.md>), [json](<https://devfeed.tech/tags/json.md>), [jsonb](<https://devfeed.tech/tags/jsonb.md>), [postgres](<https://devfeed.tech/tags/postgres.md>), [postgresql](<https://devfeed.tech/tags/postgresql.md>)

## AI overview

A practical introduction to using JSONB in PostgreSQL, covering GIN indexing, JSONB operators, querying nested values, filtering by JSON content, and pretty-printing results.

## Source excerpt

JSONB is an awesome datatype in Postgres. I find myself using it on a weekly basis these days. Often in using some API (such as clearbit) I'll get a JSON response back, instead of parsing that out into a table structure it's really easy to throw it into a JSONB then query for various parts of it. If you're not familiar with JSONB, it's a binary representation of JSON in your database. You can read a bit more about it vs. JSON here. In working with JSONB here's a few quick tips to get up and running with it even faster: Indexing For the most part you don't have to think to much about this. With Postgres powerful indexing types you can add one index and have everything within the JSON document, all the keys and all the values, automatically indexed. The key here is to add a GIN index. Once this is done queries should be much faster where you're searching for some value: CREATE INDEX idx_data ON companies USING GIN (data); Querying Querying is a little bit more work, but once you get the basics it can be pretty straight forward. There's a few new operators you'll want to quickly ramp up on and from there querying becomes easy. For the most basic part you now have an operator so traverse down the various keys. First let's get some idea of what the JSON looks like so we can have something to work with. Here's a sample set of data that we get back from Clearbit: { "domain": "citusdata.com", "company": { "id": "b1ff2bdf-0d8d-4d6d-8bcc-313f6d45996a", "url": "http:\/\/citusdata.com", "logo": "https:\/\/logo.clearbit.com\/citusdata.com", "name": "Citus Data", "site": { "h1": null, "url": "http:\/\/citusdata.com", "title": "Citus Data", }, "tags": [ "SAAS", "Enterprise", "B2B", "Information Technology & Services", "Technology", "Software" ], "domain": "citusdata.com", "twitter": { "id": "304455171", "bio": "Builders of Citus, the extremely scalable PostgreSQL database.", "site": "https:\/\/t.co\/hKpZjIy7Ej", "avatar": "https:\/\/pbs.twimg.com\/profile_images\/63090046899510886