我有一个json文件,我需要拆分部件并将每个部件分类到他的列。
这是我的表:
create table json_data(
row_id int,
id varchar PRIMARY KEY,
created_at timestamp ,
closed_at timestamp ,
open boolean,
num_changes int,
user_name varchar,
uid int,
min_lat real,
max_lat real,
min_lon real,
max_lon real,
created_by varchar,
imagery_used varchar,
version varchar,
comment varchar
);
这是我的Json文件示例:
{"id":"19739143","created_at":"2013-12-31T22:28:28Z","closed_at":"2013-12-31T23:59:11Z","open":"false","num_changes":"81","user":"47NOE","uid":"1811738","min_lat":"18.0813259","max_lat":"18.1163397","min_lon":"-63.0392394","max_lon":"-63.0205645","created_by":"Potlatch 2","build":"2.3-605-gbec0ea7","version":"2.3"}
{"id":"19739670","created_at":"2013-12-31T23:59:13Z","closed_at":"2013-12-31T23:59:15Z","open":"false","num_changes":"17","user":"mituc","uid":"1860759","min_lat":"37.0432953","max_lat":"37.0619236","min_lon":"-76.3896156","max_lon":"-76.3625439","created_by":"iD 1.3.4","imagery_used":"Bing"}
我已经尝试了以下代码,但如果缺少其中一个部分(例如“open:”for examlpe),则所有数据都向左滑动并显示错误的答案:
SELECT trim('{id:' from split_part(data, ',', 1)) AS id
,trim('created_at:' from split_part(data, ',', 2)) AS created_at
,trim('closed_at:' from split_part(data, ',', 3)) AS closed_at
,trim('open:' from split_part(data, ',', 4))AS open
,trim('num_changes:' from split_part(data, ',', 5)) AS num_changes
,trim('user:' from split_part(data, ',', 6)) AS user_name
,trim('uid:' from split_part(data, ',', 7)) AS uid
,trim('min_lat:' from split_part(data, ',', 8)) AS min_lat
,trim('max_lat:' from split_part(data, ',', 9)) AS max_lat
,trim('min_lon:' from split_part(data, ',', 10)) AS min_lon
,trim('max_lon:' from split_part(data, ',', 11)) AS max_lon
,trim('created_by:' from split_part(data, ',', 12)) AS col12
,trim('build:' from split_part(data, ',', 13)) AS col13
,trim('comment:' from split_part(data, ',', 13)) AS col13
,trim('imagery_used:' from split_part(data, ',', 13)) AS col13
,trim('}' from trim('version:' from split_part(data, ',', 14))) AS col14
FROM json_data_raw;
或
SELECT trim('{id:' from split_part(data, ',', 1)) AS id
,trim('created_at:' from split_part(data, ',', 2)) AS created_at
,case
when substring(split_part(data, ',', 3) from 1 for 10) = 'closed_at:'
then trim('closed_at:' from split_part(data, ',', 3))
when substring(split_part(data, ',', 4) from 1 for 10) = 'closed_at:'
then trim('closed_at:' from split_part(data, ',', 3))
when substring(split_part(data, ',', 5) from 1 for 10) = 'closed_at:'
then trim('closed_at:' from split_part(data, ',', 3))
else null
end
AS closed_at
,case
when substring(split_part(data, ',', 4) from 1 for 5) = 'open:'
then trim('open:' from split_part(data, ',', 4))
else null
end
AS open
,case
when substring(split_part(data, ',', 5) from 1 for 12) = 'num_changes:'
then trim('num_changes:' from split_part(data, ',', 5))
else null
end
AS num_changes
,case
when substring(split_part(data, ',', 6) from 1 for 4) = 'user:'
then trim('user:' from split_part(data, ',', 6))
else null
end
AS user
,case
when substring(split_part(data, ',', 7) from 1 for 4) = 'uid:'
then trim('uid:' from split_part(data, ',', 7))
else null
end
AS uid
,case
when substring(split_part(data, ',', 8) from 1 for 8) = 'min_lat:'
then trim('min_lat:' from split_part(data, ',', 8))
else null
end
AS min_lat
,case
when substring(split_part(data, ',', 9) from 1 for 8) = 'max_lat:'
then trim('max_lat:' from split_part(data, ',', 9))
else null
end
AS max_lat
,case
when substring(split_part(data, ',', 10) from 1 for 8) = 'min_lon:'
then trim('min_lon:' from split_part(data, ',', 10))
else null
end
AS min_lon
,case
when substring(split_part(data, ',', 11) from 1 for 8) = 'max_lon:'
then trim('min_lon:' from split_part(data, ',', 11))
else null
end
AS max_lon
,case
when substring(split_part(data, ',', 12) from 1 for 11) = 'created_by:'
then trim('created_by:' from split_part(data, ',', 12))
else null
end
AS created_by
,case
when substring(split_part(data, ',', 13) from 1 for 6) = 'build:'
then trim('build:' from split_part(data, ',', 13))
else null
end
AS build
,case
when substring(split_part(data, ',', 14) from 1 for 8) = 'version:'
then trim('version:' from split_part(data, ',', 14))
else null
end
AS version_n
FROM json_data_raw;
我如何对它进行排序,以便每个部分都排序到正确的列。
答案 0 :(得分:0)
看起来您正在使用split_part
等字符串操作函数解析JSON。但Postgres有native JSON support。您可以使用::json
将字符串转换为JSON。您可以查询->>'id'
之类的JSON对象。例如:
select data::json->>'id' as id
, data::json->>'created_at' as created_at
from json_data_raw