gpt4 book ai didi

sql - node.js - 如何将 json 数组中的值插入到 sql 数据库中

转载 作者:行者123 更新时间:2023-11-29 12:39:44 25 4
gpt4 key购买 nike

我有传入的 json 结构,如

{
"type":1,
"location":[
{"lattitude":"0", "longitude":"0"},
{"lattitude":"0", "longitude":"0"},
{"lattitude":"0", "longitude":"0"}]
}

我需要把它插入到数据库中

|------|-----------|-----------|
| type | lattitude | longitude |
|------|-----------|-----------|
| 1 | 0 | 0 |
|------|-----------|-----------|
| 1 | 0 | 0 |
|------|-----------|-----------|
| 1 | 0 | 0 |
|------|-----------|-----------|

我如何解析 json 并构建 sql 查询?

最佳答案

如果你想使用 Postgres 解决方案,你可以这样做

--INSERT INTO yourtab( type,lattitude,longitude)

select jsoncol->>'type' , j->>'lattitude'
j->>'longitude'
from
( values (:yourjsonstr :: jsonb ) ) as t(jsoncol) cross join
lateral jsonb_array_elements(jsoncol->'location')
as j;

DEMO

关于sql - node.js - 如何将 json 数组中的值插入到 sql 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56783904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com