从PostgresSQl数据库连接/构建Json对象
我试图从数据库中现有的数据构建Json数组 我将构建一个Json文件,该文件将以下Json文件与PostgresSQL相匹配从PostgresSQl数据库连接/构建Json对象,sql,json,database,postgresql,Sql,Json,Database,Postgresql,我试图从数据库中现有的数据构建Json数组 我将构建一个Json文件,该文件将以下Json文件与PostgresSQL相匹配 { "$schema": "http://json-schema.org/draft-07/schema#", "title": "Student iformation", "type": "object", "required": [ "student", "name", "login", "program", "
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Student iformation",
"type": "object",
"required": [
"student",
"name",
"login",
"program",
"branch",
"finished",
],
"properties": {
"student": {
"type": "string",
"minLength": 10,
"maxLength": 10,
"title": "A national identification number, 10 digits"
},
"name": {
"type": "string",
"title": "The name of the student"
},
"login": {
"type": "string",
"title": "The univerity issued computer login"
},
"program": {
"type": "string",
},
"branch": {
"anyOf":[{"type": "string"},{"type": "null"}],
},
"finished": {
"type": "array",
"title": "A list of read courses",
"items": {
"type": "object",
"required": [
"course",
"code",
"credits",
"grade"
],
"properties": {
"course": {
"type": "string",
"title": "Course name"
},
"code": {
"type": "string",
"minLength": 6,
"maxLength": 6,
"title": "Course code"
},
"credits": {
"type": "number",
"title": "Academic credits"
},
"grade": {
"enum" : ["U", "3", "4", "5"]
}
}
}
}
为了更好地理解如何连接、构建和排列数据库中存在的数据,我尝试了以下操作:
SELECT array_to_json(array_agg(row_to_json(t))) FROM (
SELECT idnr, name, login, program from students) t;
及
如何使用PostgresSQL构建、连接对象
select json_build_object('properties',
json_build_object('student',
json_build_object('idnr',idnr),'name',
json_build_object('name',name),'login',
json_build_object('login',login),'program',
json_build_object('program',program),'branch',
json_build_object('branch',branch)))
from Basicinformation;