如何将JSON数组转换为列和行

拉斐尔

我正在从JSON中API提取数据,其格式类似于下面的示例数据。基本上每个“行”都是一个值数组。API文档预先定义了列及其类型。所以我知道col1是例如varchar,而col2是int。

CREATE TEMP TABLE dat (data json);
INSERT INTO dat
VALUES ('{"COLUMNS":["col1","col2"],"DATA":[["a","1"],["b","2"]]}');

我想在PostgreSQL 9.3中对此进行转换,最终得到:

col1 | col2
------------
  a  |  1
  b  |  2

使用json_array_elements我可以到达:

SELECT json_array_elements(data->'DATA') 
FROM dat

json_array_elements
json
---------
["a","1"]
["b","2"]

但后来我不知道如何将JSON数组转换为PostgreSQL数组,因此我可以执行类似的操作 unnest(ARRAY['a','1'])

克雷格·林格

未知列的一般情况

得到类似的结果

col1 | col2
------------
  a  |  1
  b  |  2

将需要一堆动态SQL,因为您事先不知道列的类型,也不知道列的名称。

您可以使用以下方式解压缩json:

SELECT
  json_array_element_text(colnames, colno) AS colname,
  json_array_element_text(colvalues, colno) AS colvalue,
  rn,
  idx,
  colno
FROM (
  SELECT
    data -> 'COLUMNS' AS colnames,
    d AS colvalues,
    rn,
    row_number() OVER () AS idx
  FROM (
    SELECT data, row_number() OVER () AS rn FROM dat
  ) numbered
  cross join json_array_elements(numbered.data -> 'DATA') d
) elements
cross join generate_series(0, json_array_length(colnames) - 1) colno;

产生如下结果集:

 colname | colvalue | rn | idx | colno 
---------+----------+----+-----+-------
 col1    | a        |  1 |   1 |     0
 col2    | 1        |  1 |   1 |     1
 col1    | b        |  1 |   2 |     0
 col2    | 2        |  1 |   2 |     1
(4 rows)

然后,您可以将其用作tablefunc模块中交叉表函数的输入,例如:

SELECT * FROM crosstab('
SELECT
  to_char(rn,''00000000'')||''_''||to_char(idx,''00000000'') AS rowid,
  json_array_element_text(colnames, colno) AS colname,
  json_array_element_text(colvalues, colno) AS colvalue
FROM (
  SELECT
    data -> ''COLUMNS'' AS colnames,
    d AS colvalues,
    rn,
    row_number() OVER () AS idx
  FROM (
    SELECT data, row_number() OVER () AS rn FROM dat
  ) numbered
  cross join json_array_elements(numbered.data -> ''DATA'') d
) elements
cross join generate_series(0, json_array_length(colnames) - 1) colno;
') results(rowid text, col1 text, col2 text);

生产:

        rowid        | col1 | col2 
---------------------+------+------
  00000001_ 00000001 | a    | 1
  00000001_ 00000002 | b    | 2
(2 rows)

列名未保留在此处。

如果您使用的是9.4,则可以避免row_number()通话和使用WITH ORDINALITY,从而使其更加整洁。

用已知的固定列简化

由于您显然事先知道了列数及其类型,因此可以大大简化查询。

SELECT
  col1, col2
FROM (
  SELECT
    rn,
    row_number() OVER () AS idx,
    elem ->> 0 AS col1,
    elem ->> 1 :: integer AS col2
  FROM (
    SELECT data, row_number() OVER () AS rn FROM dat
  ) numbered
  cross join json_array_elements(numbered.data -> 'DATA') elem
  ORDER BY 1, 2
) x;

结果:

 col1 | col2 
------+------
 a    |    1
 b    |    2
(2 rows)

使用9.4 WITH ORDINALITY

如果您使用的是9.4,则可以使用WITH ORDINALITY以下命令使其更清洁

SELECT
  col1, col2
FROM (
  SELECT
    elem ->> 0 AS col1,
    elem ->> 1 :: integer AS col2
  FROM
    dat
  CROSS JOIN
    json_array_elements(dat.data -> 'DATA') WITH ORDINALITY AS elements(elem, idx)
  ORDER BY idx
) x;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章