首页 > 其他分享 >KingbaseES Json 系列七:Json记录操作函数二

KingbaseES Json 系列七:Json记录操作函数二

时间:2023-02-23 19:56:13浏览次数:55  
标签:函数 populate record bcol json cc Json KingbaseES jsonb

KingbaseES Json 系列七--Json记录操作函数二(JSONB_POPULATE_RECORD,JSONB_POPULATE_RECORDSET,JSON_POPULATE_RECORD,JSON_POPULATE_RECORDSET)

JSON 数据类型是用来存储 JSON(JavaScript Object Notation)数据的。KingbaseES为存储JSON数据提供了两种类型:JSON和 JSONB。JSON 和 JSONB 几乎接受完全相同的值集合作为输入。

本文将主要介绍Kingbase数据库的Json记录操作函数第二部分。

准备数据:

CREATE TABLE "public"."jsontable" (
	"id" integer NULL,
	"jsondata" json NULL,
	"jsonvarchar" varchar NULL,
	"jsonarray" json NULL,
	"jsonrecord" json NULL,
	"jsonset" json NULL
);

INSERT INTO "public"."jsontable" ("id","jsondata","jsonvarchar","jsonarray","jsonrecord","jsonset") VALUES
	 (1,'{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}','{"f2": {"f3": 1}, "f4": {"f5": 99, "f6": "foo"}}','[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]','{"a":1,"b":"bcol","c":"cc"}','[{"a":1,"b":"bcol","c":"cc"},{"a":1,"b":"bcol","d":""}]'),
	 (2,'{"a":[1,2,3,4,5]}','{"a": [1, 2, 3, 4, 5]}','[1,2,3,4,5]','{"a":1,"b":"bcol","c":""}','[{"a":1,"b":"bcol","c":""},{"a":1,"b":"bcol","e":""}]'),
	 (3,'{"a":1, "b": ["2", "a b"],"c": {"d":4, "e": "ab c"}}','{"a": 1, "b": ["2", "a b"], "c": {"d": 4, "e": "ab c"}}','[{"f1":1,"f2":null},2,null,3]','{"a":1,"b":"bcol","d":"dd"}','[{"a":1,"b":"bcol","c":"cc_3_1"},{"a":1,"b":"bcol","c":"cc_3_2"}]');

json函数列表

json函数简介

JSONB_POPULATE_RECORD

功能:

JSON函数,扩展 from_json 中的对象成一个行,它的列匹配由 base 定义的记录类型。在 JSONB 对象的那些与自定义类型中的列名匹配的字段,他们的值将被插入到对应的输出的列中。而 JSONB 对象中的那些没有匹配到自定类型中的列名的字段将会被忽略。

用法:

jsonb_populate_record(base anyelement,from_json jsonb)

示例:


-- 创建自定义的 SQL 类型

CREATE TYPE recordtype as (a INT, b text, c text);

--解析json数据

demo=# SELECT * FROM jsonb_populate_record(NULL::recordtype , '{"a":1,"b":"bcol","c":"c_text"}');
 a |  b   |   c    
---+------+--------
 1 | bcol | c_text
(1 行记录)

-- 数据value的类型需要能够隐式转换到record的匹配类型中

demo=# SELECT * FROM jsonb_populate_record(NULL::recordtype , '{"a":"a","b":"bcol","c":""}');
错误:  无效的类型 integer 输入语法: "a"

demo=# SELECT * FROM jsonb_populate_record(NULL::recordtype , '{"a":"1","b":"bcol","c":""}');
 a |  b   | c 
---+------+---
 1 | bcol | 
(1 行记录)

-- 数据中不存在record定义的属性时,默认返回null

demo=# SELECT * FROM jsonb_populate_record(NULL::recordtype , '{"a":1,"b":"bcol"}');       
 a |  b   | c 
---+------+---
 1 | bcol | 
(1 行记录)

-- 数据中多余的属性时,默认忽略

demo=# SELECT * FROM jsonb_populate_record(NULL::recordtype , '{"a":1,"b":"bcol","c":"c_text","d":"dcol"}'); 
 a |  b   |   c    
---+------+--------
 1 | bcol | c_text
(1 行记录)

-- 通过设置record转换类型,设置数据不存在时默认值

demo=# SELECT * FROM jsonb_populate_record((0 , 'b_default' , 'not exist' )::recordtype , '{"a":1,"b":"bcol"}');
 a |  b   |     c     
---+------+-----------
 1 | bcol | not exist
(1 行记录)

-- 从表字段中解析数据

demo=# SELECT jt.jsonrecord ,jpr.*  FROM jsontable jt , jsonb_populate_record(NULL::recordtype , jt.jsonrecord) jpr;
         jsonrecord          | a |  b   | c  
-----------------------------+---+------+----
 {"a":1,"b":"bcol","c":"cc"} | 1 | bcol | cc
 {"a":1,"b":"bcol","c":""}   | 1 | bcol | 
 {"a":1,"b":"bcol","d":"dd"} | 1 | bcol | 
(3 行记录)

-- 通过设置record转换类型,设置数据不存在时默认值

demo=# SELECT jt.jsonrecord ,jpr.*  FROM jsontable jt , jsonb_populate_record((0 , 'not exist' , 'not exist')::recordtype , jt.jsonrecord) jpr;
         jsonrecord          | a |  b   |     c     
-----------------------------+---+------+-----------
 {"a":1,"b":"bcol","c":"cc"} | 1 | bcol | cc
 {"a":1,"b":"bcol","c":""}   | 1 | bcol | 
 {"a":1,"b":"bcol","d":"dd"} | 1 | bcol | not exist
(3 行记录)

JSONB_POPULATE_RECORDSET

功能:

JSON函数,扩展 from_json 中最外层的对象数组成一个集合,它的列匹配由 base 定义的记录类型。

用法:

jsonb_populate_recordset(base anyelement,from_json jsonb)

示例:

CREATE TYPE recordtype as (a INT, b text, c text);

-- 直接解析json数据

demo=# SELECT * FROM jsonb_populate_recordset(NULL::recordtype , '[{"a":1,"b":"bcol","c":"cc"},{"a":1,"b":"bcol","c":"c_dd"}]');
 a |  b   |  c   
---+------+------
 1 | bcol | cc
 1 | bcol | c_dd
(2 行记录)

-- 数据value的类型需要能够隐式转换到record的匹配类型中

demo=# SELECT * FROM jsonb_populate_recordset(NULL::recordtype , '[{"a":"a","b":"bcol","c":"cc"},{"a":1,"b":"bcol","d":""}]');
错误:  无效的类型 integer 输入语法: "a"

demo=# SELECT * FROM jsonb_populate_recordset(NULL::recordtype , '[{"a":1,"b":"bcol","c":"cc"},{"a":1,"b":"bcol","d":""}]');  
 a |  b   | c  
---+------+----
 1 | bcol | cc
 1 | bcol | 
(2 行记录)

-- 数据中不存在record定义的属性时,默认返回null
-- 数据中多余的属性时,默认忽略

demo=# SELECT * FROM jsonb_populate_recordset(NULL::recordtype , '[{"a":1,"b":"bcol","c":"cc"},{"a":1,"b":"bcol","d":""}]');
 a |  b   | c  
---+------+----
 1 | bcol | cc
 1 | bcol | 
(2 行记录)

-- 通过设置record转换类型,设置数据不存在时默认值

demo=# SELECT * FROM jsonb_populate_recordset((0 , 'b_default' , 'not exist' )::recordtype , '[{"a":1,"b":"bcol","c":"cc"},{"a":1,"b":"bcol","d":""}]');
 a |  b   |     c     
---+------+-----------
 1 | bcol | cc
 1 | bcol | not exist
(2 行记录)

-- 从表字段中解析数据

demo=# SELECT jt.jsonset ,jpr.*  FROM jsontable jt , jsonb_populate_recordset(NULL::recordtype , jt.jsonset) jpr;
                              jsonset                              | a |  b   |   c    
-------------------------------------------------------------------+---+------+--------
 [{"a":1,"b":"bcol","c":"cc"},{"a":1,"b":"bcol","d":""}]           | 1 | bcol | cc
 [{"a":1,"b":"bcol","c":"cc"},{"a":1,"b":"bcol","d":""}]           | 1 | bcol | 
 [{"a":1,"b":"bcol","c":""},{"a":1,"b":"bcol","e":""}]             | 1 | bcol | 
 [{"a":1,"b":"bcol","c":""},{"a":1,"b":"bcol","e":""}]             | 1 | bcol | 
 [{"a":1,"b":"bcol","c":"cc_3_1"},{"a":1,"b":"bcol","c":"cc_3_2"}] | 1 | bcol | cc_3_1
 [{"a":1,"b":"bcol","c":"cc_3_1"},{"a":1,"b":"bcol","c":"cc_3_2"}] | 1 | bcol | cc_3_2
(6 行记录)

-- 通过设置record转换类型,设置数据不存在时默认值

demo=# SELECT jt.jsonset ,jpr.*  FROM jsontable jt , jsonb_populate_recordset((0 , 'not exist' , 'not exist')::recordtype , jt.jsonset) jpr;
                              jsonset                              | a |  b   |     c     
-------------------------------------------------------------------+---+------+-----------
 [{"a":1,"b":"bcol","c":"cc"},{"a":1,"b":"bcol","d":""}]           | 1 | bcol | cc
 [{"a":1,"b":"bcol","c":"cc"},{"a":1,"b":"bcol","d":""}]           | 1 | bcol | not exist
 [{"a":1,"b":"bcol","c":""},{"a":1,"b":"bcol","e":""}]             | 1 | bcol | 
 [{"a":1,"b":"bcol","c":""},{"a":1,"b":"bcol","e":""}]             | 1 | bcol | not exist
 [{"a":1,"b":"bcol","c":"cc_3_1"},{"a":1,"b":"bcol","c":"cc_3_2"}] | 1 | bcol | cc_3_1
 [{"a":1,"b":"bcol","c":"cc_3_1"},{"a":1,"b":"bcol","c":"cc_3_2"}] | 1 | bcol | cc_3_2
(6 行记录)

JSON_POPULATE_RECORD

功能:

JSON函数,扩展 from_json 中的对象成一个行,它的列匹配由 base 定义的记录类型。

用法:

json_populate_record(base anyelement,from_json jsonb)

示例:

参照JSONB_POPULATE_RECORD使用示例

JSON_POPULATE_RECORDSET

功能:

JSON函数,扩展 from_json 中最外层的对象数组成一个集合,它的列匹配由 base 定义的记录类型。

用法:

json_populate_record(base anyelement,from_json jsonb)

示例:

参照JSONB_POPULATE_RECORDSET使用示例

标签:函数,populate,record,bcol,json,cc,Json,KingbaseES,jsonb
From: https://www.cnblogs.com/kingbase/p/17139782.html

相关文章

  • KingbaseES Json 系列六:Json记录操作函数一
    KingbaseESJson系列六--Json记录操作函数一(JSONB_TO_RECORD,JSONB_TO_RECORDSET,JSON_TO_RECORD,JSON_TO_RECORDSET)JSON数据类型是用来存储JSON(JavaScriptObjectN......
  • KingbaseES Json 系列五--Json数据操作函数三
    KingbaseESJson系列五:Json数据操作函数三(JSONB_SET,JSONB_INSERT,JSON_QUERY)JSON数据类型是用来存储JSON(JavaScriptObjectNotation)数据的。KingbaseES为存储JSON......
  • KingbaseES Json 系列十二:Json其他函数
    KingbaseESJson系列十二--Json其他函数(JSONB_TYPEOF,JSON_SCALAR,JSON_SERIALIZE,JSON_TYPEOF,JSON_VALUE)JSON数据类型是用来存储JSON(JavaScriptObjectNotation)数......
  • KingbaseES Json 系列十一:Json数组操作函数
    KingbaseESJson系列十一--Json数组操作函数(JSONB_ARRAY_ELEMENTS,JSONB_ARRAY_ELEMENTS_TEXT,JSONB_ARRAY_LENGTH,JSON_ARRAY_ELEMENTS,JSON_ARRAY_ELEMENTS_TEXT,JSON_......
  • 生成函数:从入门到出门
    本博客在看完《多项式:从入门到全家桶》后食用更佳。生成函数简介省流:普通生成函数:\(f(x)=\sum_ia_ix^i\)指数生成函数:\(f(x)=\sum_i\frac{a_ix^i}{i!}\)狄利克雷......
  • 【转】package.json 文件解析
     package.json文件解析每个项目的根目录下一般都会有一个package.json文件,这个文件定义了当前项目所需要的各种模块,以及项目的配置信息(比如名称、版本、许可证等)。当......
  • 构造函数中可以调用虚函数吗?
    classBase{public: Base() { Fuction(); } virtualvoidFuction() { cout<<"Base::Fuction"<<endl; }};classA:publicBase{public: A() { ......
  • 我可以从构造函数中调用虚函数吗?
    是的,但要小心。它可能不会做你期望的。在构造函数中,虚拟调用机制被禁用,因为从派生覆盖课程还没有发生。对象是从基础向上构建的,“派生前的基础”。考虑到#include<......
  • Springboot 集成 Fastjson2
    Springboot整合Fastjson2排除默认的Jackson<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>......
  • 数据库迁移:批量 JSON文件 导入 SQL Server
    --------------------------------------------------------------Beginning--------------------------------------------------------------一、问题引入在之前的博客......