瀚高数据库JSON的使用

文档用途

本文档主要用于指导数据库使用者在瀚高数据库中使用JSON。

详细信息

一、JSON介绍

JSON数据类型可以用来存储JSON( JavaScript Object Notation)数据,是一种应用十分广泛的数据类型,目前瀚高数据库中已支持JSON和JSONB两种类型,此外,在V6版本的数据库中,还添加了SQL/JSON path系列的函数。

二、JSON和JSONB的区别

1、两种类型在使用上几乎完全一致。

2、JSON会将输入的数据直接存放至数据库中,使用时需要重新解析。

3、JSONB类型是在存放时就把JSON解析成二进制格式,使用时无需再次解析。

4、JSONB支持在其上创建索引,一定程度上性能要高于JSON。

5、JSON与JSONB在存入数据库时都会进行语法检查。

三、JSON的简单使用

1、单值示例

highgo=# select '6'::json,'"HighGoDB"'::json,'true'::json,'null'::json;

 json |    json    | json | json 

------+------------+------+------

 6    | "HighGoDB" | true | null

(1 row)

2、数组示例

highgo=# select '[ "HighGo","is",1]'::json;

        json        

--------------------

 [ "HighGo","is",1]

(1 row)

3、数值示例

highgo=# select json '{"highgo":3.1415926e-27}';

           json           

--------------------------

 {"highgo":3.1415926e-27}

(1 row)



highgo=# select jsonb '{"highgo":3.1415926e-27}';

                      jsonb                       

--------------------------------------------------

 {"highgo": 0.0000000000000000000000000031415926}

(1 row)

从该示例可以看出JSONB中存储的是numeric类型,而不是浮点数。

四、简单了解SQL/JSON path 函数

1、V6中新增了哪些SQL/JSON path 函数函数

highgo=# \df jsonb_path*

                                                                     List of functions

   Schema   |          Name          | Result data type |                                    Argument data types                                    | Type 

------------+------------------------+------------------+-------------------------------------------------------------------------------------------+------

 pg_catalog | jsonb_path_exists      | boolean          | target jsonb, path jsonpath, vars jsonb DEFAULT '{}'::jsonb, silent boolean DEFAULT false | func

 pg_catalog | jsonb_path_exists_opr  | boolean          | jsonb, jsonpath                                                                           | func

 pg_catalog | jsonb_path_match       | boolean          | target jsonb, path jsonpath, vars jsonb DEFAULT '{}'::jsonb, silent boolean DEFAULT false | func

 pg_catalog | jsonb_path_match_opr   | boolean          | jsonb, jsonpath                                                                           | func

 pg_catalog | jsonb_path_query       | SETOF jsonb      | target jsonb, path jsonpath, vars jsonb DEFAULT '{}'::jsonb, silent boolean DEFAULT false | func

 pg_catalog | jsonb_path_query_array | jsonb            | target jsonb, path jsonpath, vars jsonb DEFAULT '{}'::jsonb, silent boolean DEFAULT false | func

 pg_catalog | jsonb_path_query_first | jsonb            | target jsonb, path jsonpath, vars jsonb DEFAULT '{}'::jsonb, silent boolean DEFAULT false | func

(7 rows)

2、SQL/JSON path 函数表达式的注意点

点号 . 表示引用 Json 数据的元素

方括号 [] 表示引用数组元素

Json 数据中的数组元素下标从0开始

$ 符号表示要查询的Json文本的变量

$varname 表示指定变量

@ 指在 filter 表达式中表示当前路径元素的变量

3、SQL/JSON path 函数的简单示例

创建模拟数据

CREATE TABLE highgo_jsonb ( p jsonb);



INSERT INTO highgo_jsonb (p) VALUES ('

{ "hg_name": "hg_h1",

  "highgodb" :

  {

    "segments" : [ 

      { "location":   [ 43.43, 55.56 ],

        "start time": "2020-10-14 16:22:14",

        "HR": 36

      },

      { "location":   [ 66.23, 27.51 ],

        "start time": "2020-10-14 17:16:21",

        "HR": 67

      } ]

  }

}');

取出JSONB数组中的元素并返回text类型值

highgo=# select p ->>'hg_name' from highgo_jsonb ;

 ?column? 

----------

 hg_h1

(1 row)

用jsonb_path_query()函数取出等同值

highgo=# SELECT jsonb_path_query(p,'$.hg_name') FROM highgo_jsonb ;

 jsonb_path_query 

------------------

 "hg_h1"

(1 row)

用jsonb_pretty()取出highgodb中下标0对应的segmengt值

highgo=# SELECT jsonb_pretty(jsonb_path_query(p,'$.highgodb.segments[0].location')) FROM highgo_jsonb ;

 jsonb_pretty 

--------------

 [           +

     43.43,  +

     55.56   +

 ]

(1 row)