Postgresql – do with the new json field

jsonpostgresqlpostgresql-9.2postgresql-9.3

PostgreSQL 9.2 introduced the json field type. Why and when should I use it? What benefits does it have over a text field?

I thought there were new query options available, however I haven't seen any. Am I missing something?

Best Answer

Postgres 9.2

The benefit of the new feature is two-fold. Columns of type json verify the validity of its content so that what's in the column is automatically valid JSON and you get an error if you try to write anything else to it.
And you have basic functions to create valid JSON on the fly from rows or arrays - which is a very common use-case.

I quote Andrew Dunstan on the pgsql-hackers list:

At some stage there will possibly be some json-processing (as opposed to json-producing) functions, but not in 9.2.

I used that quote before under this related question on SO.

Postgres 9.3

.. finally brings a number of functions and operators. Check out the manual page for JSON functions.

Related answer on SO:

@Will put up blog post. See comment below.

Postgres 9.4

Be sure to check out the new jsonb type with a host of new functionality.

Above all, the decomposed, binary storage allows for smaller storage on disk and an equality operator for jsonb (unlike json), which makes a number of additional operations possible (like DISTINCT or a UNIQUE index).

Yet more functions have been added for both json and jsonb. json_to_record(), json_to_recordset() etc. More in the release notes.