问题不是因为TXT是
SELECT to_jsonb(file_get_contents('/tmp/test.json'))
不是返回JSON对象,而是返回一个字符串...即使使用replace(txt,E'\n',' ')
,它也不起作用。如何规范化字符串并将其真正转换为JSON?
PS:我正在使用JSONb,它必须像JSON一样可用于提取。
注释
json文件/tmp/test.json
:
[
{
"foo": "etc","bar": "etc","x": 123
},{
"foo": "aaa","bar": "bbb","x": 456
}
]
我正在使用UBUNTU 18 LTS,PostgreSQL v12和Linux标准TXT中的文件。终端命令file -i /tmp/test.json
说,这很好,“ text / plain; charset = utf-8”。
要将全文只加载到一个字段中(对于COPY FROM来说似乎是不可能的),PostgreSQL很难看,但是此功能已经过测试并且可靠:
CREATE EXTENSION PLpython3U;
CREATE or replace FUNCTION file_get_contents(p_file text) RETURNS text AS $$
import os.path
if not os.path.isfile(args[0]):
return None
with open(args[0],"r") as content_file:
content = content_file.read()
return content
$$ LANGUAGE PLpython3U;