Json数组成行+ postgresql + node.js

时间:2017-07-27 05:36:57

标签: json node.js postgresql

考虑我有一个json数组如下

[
{"a":1,"b":2},{"a":3,"b":4} ,{"a":5,"b":6}
]

我必须将其插入postgtreSQL,如下所示:

in  out
1  2
3  4
5  6

我在postgreSQL中读到了JSON数据类型,但我无法弄清楚如何实现这一点。请分享您的想法。提前致谢。

1 个答案:

答案 0 :(得分:0)

insert into my_table (in, out)
select a, b
from jsonb_to_recordset(
    '[{"a":1,"b":2},{"a":3,"b":4} ,{"a":5,"b":6}]'
) r (a int, b int)

https://www.postgresql.org/docs/current/static/functions-json.html#FUNCTIONS-JSON-PROCESSING-TABLE

在Javascript中转义单引号:

var query = 'insert into table (enroll_id, time) select enroll_id, time from jsonb_to_recordset(\'' + data + '\') r (enroll_id bigserial, time timestamp)';