Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从PostgresSQl数据库连接/构建Json对象_Sql_Json_Database_Postgresql - Fatal编程技术网

从PostgresSQl数据库连接/构建Json对象

从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", "

我试图从数据库中现有的数据构建Json数组

我将构建一个Json文件,该文件将以下Json文件与PostgresSQL相匹配

{
  "$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;