简体   繁体   English

查询Postgres SQL JSON表

[英]Querying postgres SQL JSON table

I have a JSON column (named pracInfo) in a postgres DB that holds data in the following format: 我在postgres DB中有一个JSON列(名为pracInfo),它以以下格式保存数据:

[{"ApplicationId":["123455667"],"Domain":["WORKGROUP"],"PracName":["ABC Corp cc"],"Phone":["011 123 4567"],"EMail":["abc@abc.co.za"],"SQLversion":["Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) \n\tNov 24 2008 13:01:59 \n\tCopyright (c) 1988-2005 Microsoft Corporation\n\tExpress Edition on Windows NT 6.1 (Build 7601: Service Pack 1)\n"],"ABCversion":["0000"],"ABCDBversion":["2.604"],"ABCDDBversion":["1.0.3"]}]

I'd like to pull a query that splits out the JSON properties eg ApplicationId, Domain, PracName etc as different columns. 我想提出一个查询,该查询将JSON属性(例如ApplicationId,Domain,PracName等)拆分为不同的列。 Where the column has two objects within the array, i'd like that to be split over two rows. 如果该列在数组中有两个对象,我希望将其拆分为两行。

I've tried various query formats but the results keep returning blank, for example json_populate_recordset eg 我尝试了各种查询格式,但是结果始终返回空白,例如json_populate_recordset例如

CREATE type json_type AS (PracName varchar(100),Email Varchar(100));

SELECT id, (json_populate_recordset(null::json_type, "pracInfo")).* FROM public."PracInfos";

What am I doing wrong? 我究竟做错了什么?

You need to escape the type names with double quotes to account for case sensitivity. 您需要使用双引号对类型名称进行转义以解决大小写问题。 I also removed the length limitation that doesn't need to be set in PostgreSQL : 我还删除了不需要在PostgreSQL中设置的长度限制:

CREATE type json_type AS ("PracName" varchar, "Email" varchar);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM