From 89d1274f81b1877b4d3a26fa95bae8497d5ca74c Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Wed, 3 Jun 2015 08:37:46 -0500 Subject: [PATCH] Add Extracting Nested JSON Data as a postgres til. --- README.md | 1 + postgres/extracting-nested-json-data.md | 26 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 postgres/extracting-nested-json-data.md diff --git a/README.md b/README.md index 376afcc..8decf28 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ smart people at [Hashrocket](http://hashrocket.com/). ### postgres +- [Extracting Nested JSON Data](postgres/extracting-nested-json-data.md) - [Timestamp Functions](postgres/timestamp-functions.md) - [Toggling The Pager In PSQL](postgres/toggling-the-pager-in-psql.md) - [Turning Timing On](postgres/turning-timing-on.md) diff --git a/postgres/extracting-nested-json-data.md b/postgres/extracting-nested-json-data.md new file mode 100644 index 0000000..0dd9414 --- /dev/null +++ b/postgres/extracting-nested-json-data.md @@ -0,0 +1,26 @@ +# Extracting Nested JSON Data + +If you are storing nested JSON data in a postgres JSON column, you are +likely going to find yourself in a situation where you need to access some +of those nested values in your database code. For instance, you may need to +get at the license number in this JSON column + +```sql + owner +------------------------------------------------------------------------------ +{ "name": "Jason Borne", "license": { "number": "T1234F5G6", "state": "MA" } } +``` + +Unfortunately, the `->` operator isn't going to do the trick. You need the +`json_extract_path` function + +```sql +> select json_extract_path(owner, 'license', 'number') from some_table; + + json_extract_path +------------------- + T1234F5G6 +``` + +Read more about [JSON Functions and +Operators](http://www.postgresql.org/docs/9.4/static/functions-json.html).