在 postgresql 的 jsonb[] 类型列中插入数据的正确语法是啥

Posted

技术标签:

【中文标题】在 postgresql 的 jsonb[] 类型列中插入数据的正确语法是啥【英文标题】:What is the correct syntax of inserting the data inside the jsonb[] type column inside postgresql在 postgresql 的 jsonb[] 类型列中插入数据的正确语法是什么 【发布时间】:2021-12-22 18:12:13 【问题描述】:

既然我们只能在 jsonb 列中存储 json 对象的数组,那么为什么要使用 jsonb[]。 我试图通过简单地将 jsonb 对象数组从 jsonb 列复制到 jsonb[] 列来将数据插入 jsonb[] 中。但是出现错误“格式错误的数组文字:必须引入明确指定的数组维度”。

[
  
    "SchoolsCode": "",
    "SchoolsName": "",
    "SchoolsType": "High School",
    "ErrorMessage": null,
    "SchoolsDegree": "Doctoral Degree",
    "BiskDocumentID": "",
    "FailedAttempts": 0,
    "SchoolsDegreeID": 2,
    "ChecklistsHidden": "Active",
    "ChecklistsStatus": "Received",
    "ChecklistsSection": "Official Transcript",
    "ChecklistsSubject": "High School Transcript (Bloomingdale High School)",
    "SchoolMailingAddress": 
      "City": "Windham",
      "Region": "ME",
      "Country": "United States",
      "ZipCode": "",
      "AddressStreet1": "406 Gray Rd",
      "AddressStreet2": null
    ,
    "SchoolsConferredDate": "2018-05-01",
    "DocumentInformationID": 1,
    "SchoolsAttendedToDate": "2018-07-01",
    "SchoolsAttendedFromDate": "2014-04-01",
    "IsSalesforceUpsertSuccess": true,
    "IsStatusReceivedFromMaterialsEndpoint": true
  ,
  
    "SchoolsCode": "",
    "SchoolsName": "Bloomingdale High Scho",
    "SchoolsType": "High School",
    "ErrorMessage": null,
    "SchoolsDegree": "Doctoral Degree",
    "BiskDocumentID": "",
    "FailedAttempts": 0,
    "SchoolsDegreeID": 6,
    "ChecklistsHidden": "Active",
    "ChecklistsStatus": "Received",
    "ChecklistsSection": "Official Transcript",
    "ChecklistsSubject": "",
    "SchoolMailingAddress": 
      "City": "Windham",
      "Region": "ME",
      "Country": "United States",
      "ZipCode": "",
      "AddressStreet1": "406 Gray Rd",
      "AddressStreet2": null
    ,
    "SchoolsConferredDate": "2018-05-01",
    "DocumentInformationID": 2,
    "SchoolsAttendedToDate": "2018-07-01",
    "SchoolsAttendedFromDate": "2014-04-01",
    "IsSalesforceUpsertSuccess": true,
    "IsStatusReceivedFromMaterialsEndpoint": true
  ,
  
    "SchoolsCode": "",
    "SchoolsName": "Governors State University",
    "SchoolsType": "High School",
    "ErrorMessage": null,
    "SchoolsDegree": "Bachelor's Degree",
    "BiskDocumentID": "fwafhawolef",
    "FailedAttempts": 0,
    "SchoolsDegreeID": 4,
    "ChecklistsHidden": "Active",
    "ChecklistsStatus": "Missing",
    "ChecklistsSection": "Official Transcript",
    "ChecklistsSubject": "High School Transcript (Bloomingdale High School)",
    "SchoolMailingAddress": 
      "City": "Windham",
      "Region": "ME",
      "Country": "United States",
      "ZipCode": "04062",
      "AddressStreet1": "406 Gray Rd",
      "AddressStreet2": null
    ,
    "SchoolsConferredDate": "2018-05-01",
    "DocumentInformationID": 3,
    "SchoolsAttendedToDate": "2018-07-01",
    "SchoolsAttendedFromDate": "2014-04-01",
    "IsSalesforceUpsertSuccess": true,
    "IsStatusReceivedFromMaterialsEndpoint": true
  
]

【问题讨论】:

jsonb[] 几乎没有意义。将列的类型更改为jsonb,然后您可以直接插入您的值,没有任何问题。 为什么要使用 jsonb[] 我不知道有什么理由这样做。 是的,插入和检索过程完全适用于 jsonb 类型。 【参考方案1】:

试试这个:inserting-jsonb-data-in-postgresql-9-6

文档:该功能从 postgres 9.6 开始存在, funtions-jsonb-postgresql

简单插入也应该可以。 例如:

 INSERT INTO orders (info)
    VALUES(' "customer": "John Doe", "items": "product": "Beer","qty": 6');

如果以上不起作用,请参考此链接,此问题与您的问题相似:https://***.com/a/48866861/11094058

【讨论】:

以上是关于在 postgresql 的 jsonb[] 类型列中插入数据的正确语法是啥的主要内容,如果未能解决你的问题,请参考以下文章

使用 jsonb 数据类型的 PostgreSQL 插入查询

是否可以将 Hibernate 与 PostgreSql 的 JSONB 数据类型一起使用?

如何使用PostgreSQL中的JSONB数据类型

如何逃脱? (问号)运算符在 Rails 中查询 Postgresql JSONB 类型

JOOQ 可以为 H2/Postgresql 别名 Liquibase JSONB 数据类型吗

将 Spring Data 与 PostgreSQL JsonB 类型属性一起使用