![](/img/trans.png)
[英]Querying combination of two keys in a single JSON column in table in Postgres database
[英]Import json file into postgres table with single json column
我一直试图在postgres中将json文件作为单个json列加载。
表格: create table book(values json);
该文件如下所示:
[
{
"isbn": "846896359-3",
"title": "Jungle Book 2, The",
"price": 22.05,
"date": "12/28/2017",
"authors": [
{
"first": "Marlène",
"last": "Ashley",
"age": 38
},
{
"first": "Miléna",
"last": "Finley",
"age": 37
},
{
"first": "Stévina",
"last": "Bullus",
"age": 44
}
],
"publisher": {
"name": "Youspan",
"address": {
"street": "Iowa",
"number": "853",
"city": "München",
"country": "Germany"
},
"phone": "361-191-8111"
}
},
{
"isbn": "558973823-7",
"title": "Star Trek III: The Search for Spock",
"price": 36.58,
"date": "4/19/2017",
"authors": [
{
"first": "Uò",
"last": "Ibel",
"age": 26
},
{
"first": "Mélys",
"last": "Grasner",
"age": 36
},
{
"first": "Mylène",
"last": "Laven",
"age": 40
},
{
"first": "Pò",
"last": "Lapsley",
"age": 37
}
],
"publisher": {
"name": "Chatterbridge",
"address": {
"street": "Dennis",
"number": "1",
"city": "São Tomé",
"country": "Sao Tome and Principe"
},
"phone": "845-226-0017"
}
}
]
尝试了复制命令,但抛出了'Token "" is invalid.'
错误。 我也尝试了许多其他解决方案
所以,我终于让它工作了。 首先,我使用tr -d '\\n' < yourfile.txt
从文件中删除了换行符
然后我运行以下脚本:
create table Bookstemp(values text);
copy Bookstemp from 'BOOKS_DATANew.json';
create table books(valjson json);
Insert into books
select values
from (
select json_array_elements(replace(values,'\','\\')::json) as values
from Bookstemp
) a;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.