我有JSON数组数组
[[\"97.00000\",\"1.00000000\",1378856831.546,\"s\",\"m\",\"\"],
[\"96.99000\",\"0.00987833\",1379071035.0723,\"b\",\"m\",\"\"],
[\"96.30000\",\"0.10384000\",1379085887.0434,\"s\",\"l\",\"\"],
[\"96.20000\",\"0.10395000\",1379086063.4595,\"s\",\"l\",\"\"],
[\"96.00000\",\"0.79221000\",1379086126.0679,\"s\",\"l\",\"\"]]
我想在PostgreSQL中将每个条目插入单独的列和行中:
first(double precision)|sec(double precision)|thr(double precision)|fou(text)|fiv(text)|six(text)
97.00000 | 1.00000000 |1378856831.546 |s |m | |
96.99000 | 0.00987833 |1379071035.0723|b |m | |
.
.
如果我这样做:
INSERT INTO my_schema.my_table_second
(first_col, secon_col, third_col,
first_char_col, secon_char_col, third_char_col)
VALUES json_array_elements(
[[\"97.00000\",\"1.00000000\",1378856831.546,\"s\",\"m\",\"\"],
[\"96.99000\",\"0.00987833\",1379071035.0723,\"b\",\"m\",\"\"],
[\"96.30000\",\"0.10384000\",1379085887.0434,\"s\",\"l\",\"\"],
[\"96.20000\",\"0.10395000\",1379086063.4595,\"s\",\"l\",\"\"],
[\"96.00000\",\"0.79221000\",1379086126.0679,\"s\",\"l\",\"\"],
[\"231.00000\",\"0.02164502\",1383838824.8187,\"s\",\"l\",\"\"],
[\"224.00000\",\"0.84580357\",1383839436.6596,\"b\",\"l\",\"\"]]));
我收到错误。有人可以给出一个如何解析这个数组的例子,或者只有格式化SQL外部字符串的选项吗?
答案 0 :(得分:0)
使用\
功能删除不必要的字符replace().
此外,您应该使用select
,而不是values:
with my_data(data) as (
values
('[[\"97.00000\",\"1.00000000\",1378856831.546,\"s\",\"m\",\"\"],
[\"96.99000\",\"0.00987833\",1379071035.0723,\"b\",\"m\",\"\"],
[\"96.30000\",\"0.10384000\",1379085887.0434,\"s\",\"l\",\"\"],
[\"96.20000\",\"0.10395000\",1379086063.4595,\"s\",\"l\",\"\"],
[\"96.00000\",\"0.79221000\",1379086126.0679,\"s\",\"l\",\"\"]]')
)
insert into my_schema.my_table_second (
first_col,
secon_col,
third_col,
first_char_col,
secon_char_col,
third_char_col)
select
(arr->>0)::float,
(arr->>1)::float,
(arr->>2)::float,
arr->>3,
arr->>4,
arr->>5
from my_data
cross join json_array_elements(replace(data, '\', '')::json) arr