how do I convert text to jsonB

后端 未结 3 1279
温柔的废话
温柔的废话 2021-02-13 03:38

What is the proper way to convert any text (or varchar) to jsonB type in Postgres (version 9.6) ?

For example, here I am using two methods and I am getting

3条回答
  •  梦谈多话
    2021-02-13 04:07

    Atomic type conversion and CSV-to-JSONb

    A typical parse problem in open data applications is to parse line by line a CSV (or CSV-like) text into JSONB correct (atomic) datatypes. Datatypes can be defined in SQL jargon ('int', 'text', 'float', etc.) or JSON jargon ('string', 'number'):

    CREATE FUNCTION csv_to_jsonb(
      p_info text,           -- the CSV line
      coltypes_sql text[],   -- the datatype list
      rgx_sep text DEFAULT '\|'  -- CSV separator, by regular expression
    ) RETURNS JSONb AS $f$
      SELECT to_jsonb(a) FROM (
          SELECT array_agg(CASE
              WHEN tp IN ('int','integer','smallint','bigint') THEN to_jsonb(p::bigint)
              WHEN tp IN ('number','numeric','float','double') THEN  to_jsonb(p::numeric)
              WHEN tp='boolean' THEN to_jsonb(p::boolean)
              WHEN tp IN ('json','jsonb','object','array') THEN p::jsonb
              ELSE to_jsonb(p)
            END) a
          FROM regexp_split_to_table(p_info,rgx_sep) WITH ORDINALITY t1(p,i)
          INNER JOIN unnest(coltypes_sql) WITH ORDINALITY t2(tp,j)
          ON i=j
      ) t
    $f$ language SQL immutable;
    
    -- Example:
    SELECT csv_to_jsonb(
      '123|foo bar|1.2|true|99999|{"x":123,"y":"foo"}',
      array['int','text','float','boolean','bigint','object']
    );
    -- results  [123,   "foo bar", 1.2,    true, 99999,  {"x": 123, "y": "foo"}]
    -- that is: number, string,   number,  true, number, object
    

提交回复
热议问题