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

KingbaseES Json 系列六:Json记录操作函数一

时间:2023-02-23 19:55:54浏览次数:60  
标签:函数 record bcol text JSON cc Json KingbaseES jsonb

KingbaseES Json 系列六--Json记录操作函数一(JSONB_TO_RECORD,JSONB_TO_RECORDSET,JSON_TO_RECORD,JSON_TO_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_TO_RECORD

功能:

JSON函数,从一个JSON对象构建一个任意的记录,正如所有返回 record 的函数一样,调用者必须用一个 AS 子句显式的定义记录的结构。

用法:

jsonb_to_record(jsonb)

示例:


-- 解析json数据

demo=# SELECT * FROM jsonb_to_record('{"a":1,"b":[1,2,3],"c":[1,2,3],"e":"bar","r": {"a": 123, "b": "a b c"}}') as x(a int,b text);                  
 a |     b     
---+-----------
 1 | [1, 2, 3]
(1 行记录)

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

demo=# SELECT * FROM jsonb_to_record('{"a":1,"b":[1,2,3],"c":[1,2,3],"e":"bar","r": {"a": 123, "b": "a b c"}}') as x(a int,b text, c boolean[]);
错误:  无效的类型 boolean 输入语法: "2"

demo=# SELECT * FROM jsonb_to_record('{"a":1,"b":[1,2,3],"c":[1,2,3],"e":"bar","r": {"a": 123, "b": "a b c"}}') as x(a int,b text, c int[]);    
 a |     b     |    c    
---+-----------+---------
 1 | [1, 2, 3] | {1,2,3}
(1 行记录)

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

demo=# SELECT * FROM jsonb_to_record('{"a":1,"b":[1,2,3],"c":[1,2,3],"e":"bar","r": {"a": 123, "b": "a b c"}}') as x(a int,b text, c text[], d text);
 a |     b     |    c    | d 
---+-----------+---------+---
 1 | [1, 2, 3] | {1,2,3} | 
(1 行记录)

-- 从表字段中解析数据

demo=# select jt.jsonrecord , x.*  from  jsontable jt , jsonb_to_record(jt.jsonrecord) as x(a int , b text , c text) ;
         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 行记录)

JSONB_TO_RECORDSET

功能:

JSON函数,从一个JSON对象数组构建一个任意的记录集合,正如所有返回 record 的函数一样,调用者必须用一个 AS 子句显式的定义记录的结构。

用法:

jsonb_to_recordset(jsonb)

示例:


-- 解析json数据

demo=# SELECT * FROM jsonb_to_recordset ('[{"a":1,"b":"foo","c":true},{"a":"2","b":"bar"}]')as x(a int, b text);
 a |  b  
---+-----
 1 | foo
 2 | bar
(2 行记录)

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

demo=# SELECT * FROM jsonb_to_recordset ('[{"a":1,"b":"foo","c":true},{"a":"2","b":"bar"}]')as x(a int, b text,c int);

错误:  无效的类型 integer 输入语法: "true"

demo=# SELECT * FROM jsonb_to_recordset ('[{"a":1,"b":"foo","c":true},{"a":"2","b":"bar"}]')as x(a int, b text,c boolean);
 a |  b  | c 
---+-----+---
 1 | foo | t
 2 | bar | 
(2 行记录)

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

demo=# SELECT * FROM jsonb_to_recordset ('[{"a":1,"b":"foo","c":true},{"a":"2","b":"bar"}]')as x(a int, b text,c boolean,d int);
 a |  b  | c | d 
---+-----+---+---
 1 | foo | t |  
 2 | bar |   |  
(2 行记录)

-- 从表字段中解析数据

demo=# select jt.jsonset , x.*  from  jsontable jt , jsonb_to_recordset(jt.jsonset) as x(a int , b text , c text) ;
                              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 行记录)

JSON_TO_RECORD

功能:

JSON函数,从一个JSON对象构建一个任意的记录,正如所有返回 record 的函数一样,调用者必须用一个 AS 子句显式的定义记录的结构。

用法:

json_to_record(jsonb)

示例:

参照JSONB_TO_RECORD使用示例

JSON_TO_RECORDSET

功能:

JSON函数,从一个JSON对象数组构建一个任意的记录集合,正如所有返回 record 的函数一样,调用者必须用一个 AS 子句显式的定义记录的结构。

用法:

json_to_recordset(jsonb)

示例:

参照JSONB_TO_RECORDSET使用示例

标签:函数,record,bcol,text,JSON,cc,Json,KingbaseES,jsonb
From: https://www.cnblogs.com/kingbase/p/17139770.html

相关文章

  • 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--------------------------------------------------------------一、问题引入在之前的博客......
  • python入门之函数返回值的应用
    """函数返回值应用"""#函数设计思想:#分而治之#干一件事#需求:定义两个数字相加的函数#defadd():#1.获取数据#number01=int(i......