以下举例说明:

postgres=# select '{"b":1,"a":2}'::json;
json
---------------
{"b":1,"a":2}
(1 row)

创建json字段表:

postgres=# drop table if exists test_json1;
NOTICE: table "test_json1" does not exist, skipping
DROP TABLE
postgres=# create table test_json1(id serial primary key,name json);
CREATE TABLE

插入json数据:

postgres=# insert into test_json1(name) values('{"col1":1,"col2":"user1","col3":"male"}');
INSERT 0 1
postgres=# insert into test_json1(name) values('{"col1":2,"col2":"user2","col3":"female"}');
INSERT 0 1
postgres=# select * from test_json1;
id | name
----+-------------------------------------------
1 | {"col1":1,"col2":"user1","col3":"male"}
2 | {"col1":2,"col2":"user2","col3":"female"}
(2 rows)

获取指定key的value值:

postgres=# select name -> 'col2' from test_json1 where id = 1;
?column?
----------
"user1"
(1 row) postgres=# select name ->> 'col2' from test_json1 where id = 1;
?column?
----------
user1
(1 row)

json和jsonb的区别,先看例子:

postgres=# select ' {"a":3 ,"b":1, "a":2}'::json;
json
------------------------
{"a":3 ,"b":1, "a":2}
(1 row) postgres=# select ' {"a":3 ,"b":1, "a":2}'::jsonb;
jsonb
------------------
{"a": 2, "b": 1}
(1 row)

1,jsonb存储的是二进制类型(存储慢,获取快),json是文本类型(存储快,获取慢)。

2,jsonb可以调整键的顺序。
3,jsonb可以删除多余的空格。
4,jsonb可以删除重复的键。
判断字符是否顶层键值。

postgres=# select '{"b":"1","a":"2"}'::jsonb ? 'a';
?column?
----------
t
(1 row)

删除数据的键/值

postgres=# select '{"b":"1","a":"2"}'::jsonb - 'a';
?column?
------------
{"b": "1"}
(1 row)

json函数

1,json_each扩展最外层的json对象成为一组键/值结果集,如下所示:

postgres=# select * from json_each('{"b":"1","a":"2"}');
key | value
-----+-------
b | "1"
a | "2"
(2 rows)

2,json_each_text以文本返回结果,如下所示:

postgres=# select * from json_each_text('{"b":"1","a":"2"}');
key | value
-----+-------
b | 1
a | 2
(2 rows)

3,row_to_json普通表转为json格式:

postgres=# drop table if exists test_copy;
NOTICE: table "test_copy" does not exist, skipping
DROP TABLE
postgres=# create table test_copy as select 1 as id,'a' as name;
WARNING: column "name" has type "unknown"
DETAIL: Proceeding with relation creation anyway.
SELECT 1
postgres=# select * from test_copy where id = 1;
id | name
----+------
1 | a
(1 row) postgres=# select row_to_json(test_copy) from test_copy where id = 1;
row_to_json
---------------------
{"id":1,"name":"a"}
(1 row)

4,json_object_keys返回最外层的json对象中的键的集合

postgres=# select * from json_object_keys('{"b":"1","a":"2"}');
json_object_keys
------------------
b
a
(2 rows)

json键/值的追加“||”

postgres=# select '{"b":"1","a":"2"}'::jsonb||'{"d":"3","c":"4"}'::jsonb;
?column?
------------------------------------------
{"a": "2", "b": "1", "c": "4", "d": "3"}
(1 row)

json键/值的删除“-”

postgres=# select '{"b":"1","a":"2"}'::jsonb - 'a';
?column?
------------
{"b": "1"}
(1 row)

删除嵌套json数据“#-”

postgres=# select '["a","b","c"]'::jsonb - 0;
?column?
------------
["b", "c"]
(1 row) postgres=# select '{"name":"bob","contact":{"phone1":"1234","phone2":"5678"}}'::jsonb #- '{contact,phone1}'::text[];
?column?
------------------------------------------------
{"name": "bob", "contact": {"phone2": "5678"}}
(1 row) postgres=# select '{"name":"bob","contact":["phone1","phone2","phone3"]}'::jsonb #- '{contact,0}'::text[];
?column?
--------------------------------------------------
{"name": "bob", "contact": ["phone2", "phone3"]}
(1 row)

json值的更新,jsonb_set函数,
格式:jsonb_set(target jsonb,path text[],new_value jsonb[, create_missing boolean])
target指源jsonb数据,path指路径,new_value指更新后的键值,create_missing值为true表示键不存在则添加,为false表示如果键不存在则不添加

postgres=# select jsonb_set('{"name":"bob","age":"27"}'::jsonb,'{age}','"28"'::jsonb,false);
jsonb_set
------------------------------
{"age": "28", "name": "bob"}
(1 row) postgres=# select jsonb_set('{"name":"bob","age":"27"}'::jsonb,'{age}','"28"'::jsonb,true);
jsonb_set
------------------------------
{"age": "28", "name": "bob"}
(1 row) postgres=# select jsonb_set('{"name":"bob","age":"27"}'::jsonb,'{sex}','"male"'::jsonb,false);
jsonb_set
------------------------------
{"age": "27", "name": "bob"}
(1 row) postgres=# select jsonb_set('{"name":"bob","age":"27"}'::jsonb,'{sex}','"male"'::jsonb,true);
jsonb_set
---------------------------------------------
{"age": "27", "sex": "male", "name": "bob"}
(1 row)

参考:
https://www.postgresql.org/docs/9.4/functions-json.html

pg的json类型的更多相关文章

  1. PG 中 JSON 字段的应用

    13 年发现 pg 有了 json 类型,便从 oracle 转 pg,几年下来也算比较熟稔了,总结几个有益的实践. 用途一:存储设计时无法预料的文档性的数据.比如,通常可以在人员表准备一个 json ...

  2. springmvc接收JSON类型的数据

    1.在使用AJAX传递JSON数据的时候要将contentType的类型设置为"application/json",否则的话会提示415错误 2.传递的data需要时JSON类型的 ...

  3. SpringMVC——对Ajax的处理(包含 JSON 类型)

    一.首先要搞明白的一些事情. 1.从客户端来看,需要搞明白: (1)要发送什么样格式的 JSON 数据才能被服务器端的 SpringMVC 很便捷的处理,怎么才能让我们写更少的代码,如何做好 JSON ...

  4. Struts2+Jquery实现ajax并返回json类型数据

    来源于:http://my.oschina.net/simpleton/blog/139212 摘要 主要实现步骤如下: 1.JSP页面使用脚本代码执行ajax请求 2.Action中查询出需要返回的 ...

  5. JSON.stringify实例应用—将对象转换成JSON类型进行AJAX异步传值

    在上一篇中,对JSON.stringify()方法有了初步的认识,并且做了一些简单的例子.本篇将进一步将JSON.stringify用在复杂些的实例中,例如如下需求: 在进jQuery AJAX异步传 ...

  6. 已知json类型根据类型封装集合

    1编写帮助类根绝url得到json public static string Post(string url) { string strURL = url; //创建一个HTTP请求 HttpWebR ...

  7. 转载:Struts2+Jquery实现ajax并返回json类型数据

    摘要: 主要实现步骤如下: 1.JSP页面使用脚本代码执行ajax请求 2.Action中查询出需要返回的数据,并转换为json类型模式数据 3.配置struts.xml文件 4.页面脚本接受并处理数 ...

  8. json进阶(一)js读取解析JSON类型数据

    js读取解析JSON类型数据 一.什么是JSON? JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式,同 ...

  9. HTML基础之JS中的序列化和反序列化-----字符串的json类型与字典之间的相互转换

    前端向后端传递数据的时候不能直接传递对象(如,字典),只能发字符串,Jason就是一种字符串所以前端向后端发送数据的时候,需要将对象转换成字符串 如果前端向后端发送的是json类型,需要通过JSON. ...

  10. mysql 5.7 laravel json类型数据相关操作

    2018年10月16日18:14:21 官方文档中文翻译版 原文:https://dev.mysql.com/doc/refman/5.7/en/json.html 最后有部分实例和一个小总结 11. ...

随机推荐

  1. pytest+allure接口自动化

    python类库依赖: pip3 install pytestpip3 install allure-pytestpip3 install rerequestspip3 install pytest- ...

  2. GUI程序中使用Write语句调试

    Lazarus GUI程序中使用Write语句调试 比如像VB 下的 Debug.print 可直接在立即窗口中打印出调试内容 其实可以使用 WriteLn('XXXX ', XXX); Write( ...

  3. Chrome禁用开发者工具

    在一次工作中,所做的项目要求页面中不能右击,不能打开F12.一般来说可以禁用F12的按键,但是可以通过开发者工具进入.经过个人实验,以下方法适用于谷歌浏览器.火狐浏览器,以及使用谷歌内核的浏览器(如Q ...

  4. FreeRdp(一):主要数据结构

    /** RDP连接实例 * 这是创建连接后的客户端选项 * 数据结构申请内存调用freerdp_new,释放为freerdp_free */ struct rdp_freerdp { ALIGN64 ...

  5. QT--弹出新的对话框 show()/exec()的区别

    show()显示非模态对话框,exec()显示模态对话框. 非模态对话框不会阻塞程序的线程,因此 如果你的对话框时创建在栈上,跳出作用域之后,对象便销毁了,对话框会一闪而过: 如果使用new在堆上创建 ...

  6. windows 系统进行升级之后,如何删除 Windows.old 文件夹。

    首先用鼠标右键点击 C 盘,在弹出的下拉列表里找到"属性"并点击它. 步骤阅读 2 在打开的 C 盘属性里的常规找到"磁盘清理",点击它打开. 步骤阅读 3 这 ...

  7. hive元起动报错:Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;Ljava/lang/Object;)V

    错误原因: 1.系统找不到这个类所在的jar包 2.jar包的版本不一样系统不知道使用哪个. hive启动报错的原因是后者 解决办法: 1.com.google.common.base.Precond ...

  8. [转]c#特性(Attribute)学习总结1

    特性是用于在运行时传递程序中各种元素(类.方法.结构.枚举.组件等)的行为信息的声明性标签. 官方的解读不好理解,举个常用的例子,平时会在类的上面加上[Serializable],Serializab ...

  9. GoLang之ACM控制台输入输出

    转自:https://blog.csdn.net/weixin_52690231/article/details/125436414

  10. faker函数造数据

    from faker import Fakerimport timeimport datetimet = time.time()import random# fake= Faker()# fake.n ...