JSON_TABLE() will be in PostgreSQL 17! It almost made it two years ago but was yanked away at the last minute. We will get it shortly when PostgreSQL 17 is officially released this year! Please see this.

Why is this important?

JSON has become the data interchange format of choice for most developers. JSON had a litany of features that will not be repeated here. But dealing with JSON in a relational database can be, at best, tricky.

Keeping incoming data in JSON has a lot of merit but you end up dealing with JSON handling functions that can be less than intuitive. Or you can extract the data into a relational column for high-speed access, the power of SQL, and the ability to index but the data is no longer in JSON format. Sometimes, you have the data in two places at the same time, incurring the misery of keeping both current.

But what if temporarily that JSON data was converted into a relational table? Your original data is still in JSON format. You can use the power of SQL commands with this temporarily structured data.

You can with JSON_TABLE()!

JSON_TABLE()

MySQL added JSON_TABLE() several years ago, and it is popular. Oracle and SQL Server also have it. And soon, so will PostgreSQL.

You need to tell JSON_TABLE() the name of the JSON document, the document path, and how you want to cast the JSON values. The following example shows the use of JSON_TABLE() with MySQL. Expect PostgreSQL 17 examples when that version is released.

The first example shows how the JSON value stored under the key of ‘Name’ is cast as a char(20) and now called country_name. And how the IndepYear key/value becomes an integer known as IndyYear. The output is passed to Structured Query Language for processing.  The SQL here is simple but it could be Window Functions oranalytics.

Missing data can be easily dealt with, as seen below:

Sadly, the JSON_TABLE() in PostgreSQL will be coming without the ability to handle nested columns. That becomes handy when provided with data like this:

The NESTED PATH operator allows access to each of the grades.

Conclusion

JSON_TABLE() is a great addition to PostgreSQL 17. Those of us who deal with lots of JSON-formatted data will make heavy use of it. Hopefully, in the future, this new addition will evolve, and things like JSON schema validation can be included too.

This is great news. Thank you to all the contributors involved.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments