I have two tables called book
and author
.
create table book(bookid BIGSERIAL,
bookname VARCHAR,
book_type VARCHAR,
book_pub_date DATE
);
create table Author(Authorid BIGSERIAL,
Authorname VARCHAR,
Author_emailadddr VARCHAR,
Author_phone integer
);
Application will pass the data in JSON format thru stored function. So how to store the JSON data into a tables?
Input data (Application will pass the following data thru stored function):
{
"bookid" : "1",
"bookname" : "sample book",
"book type" : "history",
"book_pub_date" : "2015-09-08",
"Author" : [{
"Authorname" : "xyz",
"Author_emailadddr" : "xyz@gmail.com",
"Author_phone" : "080-99978754"
}
]
}
Is it possible to parse the json data into a tables?
(I'm using PostgreSQL 9.2).
I think you might need Postgres 9.3+ for a pure sql solution. 9.2 has limited json functionality compared to newer versions. http://www.postgresql.org/docs/9.2/static/functions-json.html
In PostgreSQL 9.2 you will need to use a function written with pl/perl, pl/python, etc, using the relevant JSON decoding support for that language, and using the SPI to then insert the extracted data into tables. Use, eg Python's json
module or Perl's JSON
module to extract the fields, then the relevant SPI calls to create SQL to do the inserts based on the extracted data.
See:
In particular, note the sections of the plperl and plpython docs for interacting with the database and running queries.
It's really not much different to doing it with an external script.
In PostgreSQL 9.4 and above you could start to use PostgreSQL's own json support to extract parts of the json in pl/pgsql and insert
the extracted sections. No more need for plperl or plpython.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.