How to create index on JSON field in Postgres?

JsonPostgresqlIndexingPostgresql 9.3

Json Problem Overview


In PostgreSQL 9.3 Beta 2 (?), how do I create an index on a JSON field? I tried it using the -> operator used for hstore but got the following error:

 CREATE TABLE publishers(id INT, info JSON);
 CREATE INDEX ON publishers((info->'name'));

> ERROR: data type json has no default operator class for access method > "btree" HINT: You must specify an operator class for the index or > define a default operator class for the data type.

Json Solutions


Solution 1 - Json

Found:

CREATE TABLE publishers(id INT, info JSON); 
CREATE INDEX ON publishers((info->>'name'));

As stated in the comments, the subtle difference here is ->> instead of ->. The former one returns the value as text, the latter as a JSON object.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrlibView Question on Stackoverflow
Solution 1 - JsonrlibView Answer on Stackoverflow