json select question

Started by CS DBAover 9 years ago3 messagesgeneral
Jump to latest
#1CS DBA
cs_dba@consistentstate.com

All;

I'm working with a client running postgres 9.2, they have a table with a
"json_data_string" column of type json

the data looks something like this with lots of rows for each (i.e. lots
of json_data_string->book_name rows, lots of
json_data_string->catalog_name rows, etc:

|'{ "book_name": "Book the Fourth", "author": { "first_name": "Charles",
"last_name": "Davis" } }' ||'{ "catalog_name": "Catalog the Fourth", "author": { "first_name":
"Charles", "last_name": "Davis" } }'|
|'{ "magazine_name": "mag4", "author": { "first_name": "Charles",
"last_name": "Davis" } }' |||'{ "category_name": "region", "author": { "first_name": "Charles",
"last_name": "Davis" } }'||

How can i pull a unique list of all json column names? such as book_name, catalog_name, etc

Thanks in advance

#2John R Pierce
pierce@hogranch.com
In reply to: CS DBA (#1)
Re: json select question

On 9/21/2016 4:54 PM, CS DBA wrote:

How can i pull a unique list of all json column names? such as book_name, catalog_name, etc

try json_object_keys() ...
https://www.postgresql.org/docs/current/static/functions-json.html

but this will only pull the top level keys, like (from the first row of
your example) , book_name, author... it won't pull out any second level
fields like first_name, last_name.

you'll likely need to run this on all rows, and do a group by to get
what you need.

thats a pretty messy way of storing data, btw, and not very relational,
having a key name indicate a record type, and tossing all the record
types into the same table.

--
john r pierce, recycling bits in santa cruz

#3FarjadFarid(ChkNet)
farjad.farid@checknetworks.com
In reply to: CS DBA (#1)
Re: json select question

This is probably the start of your quest into this project. No doubt you’ll need to handle other queries.

So my suggestion is first to rationalise the data storage before digging any deeper.

From: pgsql-general-owner@postgresql.org [mailto:pgsql-general-owner@postgresql.org] On Behalf Of CS DBA
Sent: 22 September 2016 00:54
To: pgsql-general <pgsql-general@postgresql.org>
Subject: [GENERAL] json select question

All;

I'm working with a client running postgres 9.2, they have a table with a "json_data_string" column of type json

the data looks something like this with lots of rows for each (i.e. lots of json_data_string->book_name rows, lots of json_data_string->catalog_name rows, etc:

'{ "book_name": "Book the Fourth", "author": { "first_name": "Charles", "last_name": "Davis" } }'
'{ "catalog_name": "Catalog the Fourth", "author": { "first_name": "Charles", "last_name": "Davis" } }'
'{ "magazine_name": "mag4", "author": { "first_name": "Charles", "last_name": "Davis" } }'
'{ "category_name": "region", "author": { "first_name": "Charles", "last_name": "Davis" } }'

How can i pull a unique list of all json column names? such as book_name, catalog_name, etc

Thanks in advance