PostgreSQL 9.2 - Convert TEXT json string to type json/hstore

前端 未结 6 970
难免孤独
难免孤独 2021-01-31 07:15

I have a TEXT column containing valid JSON string.

CREATE TABLE users(settings TEXT);

INSERT INTO users VALUES (\'{\"language\":\"en\",\"gender\":\         


        
6条回答
  •  天涯浪人
    2021-01-31 08:20

    If you need an index on it, create an immutable function that takes the json as input and yields the field you want as output in a pl language, e.g.:

    create function extract_language(text) returns text as $$
      -- parse $1 as json
      -- return $1.language
    $$ language whatever immutable;
    

    Then add an index on the expression:

    create index users_language on users(extract_language(settings));
    

    The index will then (potentially) get used in queries such as:

    select * from users where extract_language(settings) = 'en';
    

提交回复
热议问题