PostgreSQL 9.4 JSON Queries
Intro
Querying JSON with SQL is extremely powerful and convenient. Some great things about:
- Use SQL to query unstructured data
- Join relational and JSON tables
- Convert between JSON and relational schema
But query writing can be difficult and non obvious at first. Official documentation doesn’t have many samples. Many useful queries need other great but not widely known features of PosgresSQL like LATERAL joins and recursive queries.
This tutorial has some real world examples.
Get some data
Lets use GitHub Archive as source for large JSON with complex structure:
wget http://data.githubarchive.org/2015-01-01-15.json.gz gzip -d 2015-01-01-15.json.gz
Load JSON to PostgreSQL
Super easy:
COPY github FROM 'c:\temp\2015-01-01-15.json' WITH (format csv, quote e'\x01', delimiter e'\x02', escape e'\x01')
Query returned successfully: 11351 rows affected, 1025 ms execution time.
Do some simple queries
Still straightforward:
select js->>'type', count(*) from github group by [...]