我想以
的形式加载格式化的JSON文件{
"EId":"104111",
"Category":"(0)",
"Mac":"ABV",
"Path":"chemin2",
"ID":"System.Byte"
}
首先创建一个带有json列的临时表,
create temporary table temp_json (values json);
copy temp_json from '/path_to_the_file/test.json';
select values->>'EId' as EId,
values->>'Category' as Category,
values->>'Mac' as Mac,
values->>'Path' as Path,
values->>'ID' as ID
from(
select json_array_elements(values) as values
from temp_json
) a;
但它显示以下消息:
ERROR: invalid input syntax for type JSON
DETAIL: The input string ended unexpectedly.
CONTEXT: JSON data, line 1: {
COPY temp_json, line 1, column values: "{"
一旦我擦除了所有空格,指令就会没有错误传递。
答案 0 :(得分:3)
假设这样的文件:
{
"EId":"104111",
"Category":"(0)",
"Mac":"ABV",
"Path":"chemin2",
"ID":"System.Byte"
}
{
"EId":"104112",
"Category":"(1)",
"Mac":"CBV",
"Path":"chemin3",
"ID":"System.Byte"
}
临时表将接收不是json的文本:
create temporary table temp_json (values text);
\copy temp_json from '/path_to/input.json';
权威表将有一个json列:
create table t (obj jsonb);
一些字符串操作:
insert into t (obj)
select
regexp_split_to_table(
replace(v, $$"}{"$$, $$"}djue748wBc,l;09{"$$),
'djue748wBc,l;09'
)::jsonb
from (
select string_agg(values, '') as v
from temp_json
) s;
obj
--------------------------------------------------------------------------------------------
{"ID": "System.Byte", "EId": "104111", "Mac": "ABV", "Path": "chemin2", "Category": "(0)"}
{"ID": "System.Byte", "EId": "104112", "Mac": "CBV", "Path": "chemin3", "Category": "(1)"}
答案 1 :(得分:2)