json和jsonb的操作符

jsonb额外操作符

json创建函数

json处理函数

函数 返回类型 描述 示例 结果

json_array_length(json)

jsonb_array_length(jsonb)

int  返回Json数组最外层元素个数  select json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');  5

json_each(json)

jsonb_each(jsonb)

setof key text, value json

setof key text, value jsonb

 将最外层Json对象转换为键值对集合  select json_each('{"a":"foo", "b":"bar"}');

(a,"""foo""")
(b,"""bar""")

json_each_text(json)

jsonb_each_text(jsonb)

setof key text, value text  将最外层Json对象转换为键值对集合,且value为text类型  select json_each_text('{"a":"foo", "b":"bar"}');

(a,foo)
(b,bar)

json_extract_path(from_json json,

VARIADIC path_elems text[])

jsonb_extract_path(from_json jsonb,

VARIADIC path_elems text[])

json

jsonb

 返回path_elems指向的value,同操作符#>  select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}','f4');  {"f5":99,"f6":"foo"}

json_extract_path_text(from_json json,

VARIADIC path_elems text[])

jsonb_extract_path_text(from_json jsonb,

VARIADIC path_elems text[])

text   返回path_elems指向的value,并转为text类型,同操作符#>>  select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}','f4', 'f6');  foo

json_object_keys(json)

jsonb_object_keys(jsonb)

setof text  返回json对象最外层的key  select json_object_keys('{"f1":"abc","f2":{"f3":"a", "f4":"b"}}');

f1
f2

json_populate_record(base anyelement,

from_json json)

jsonb_populate_record(base anyelement,

from_json jsonb)

anyelement  将json对象的value以base定义的行类型返回,如果行类型字段比json对象键值少,则多出的键值将被抛弃;如果行类型字段多,则多出的字段自动填充NULL。

表tbl_test定义:

Table "public.tbl_test"
Column | Type | Modifiers 
--------+-----------------------+-----------
a | bigint | 
b | character varying(32) |

c | character varying(32) |

select * from json_populate_record(null::tbl_test, '{"a":1,"b":2}');

a |  b |  c 
---+---+------
1 | 2  | NULL

json_populate_recordset(base anyelement,

from_json json)

jsonb_populate_recordset(base anyelement,

from_json jsonb)

setof anyelement  将json对象最外层数组以base定义的行类型返回

表定义同上

select * from json_populate_recordset(null::tbl_test, '[{"a":1,"b":2},{"a":3,"b":4}]');

a | b |  c 
---+---+------
1 | 2 | NULL
3 | 4 | NULL

json_array_elements(json)

jsonb_array_elements(jsonb)

setof json

setof jsonb

 将json数组转换成json对象value的集合  select json_array_elements('[1,true, [2,false]]');

1
true
[2,false]

json_array_elements_text(json)

jsonb_array_elements_text(jsonb)

setof text  将json数组转换成text的value集合  select json_array_elements_text('["foo", "bar"]');

foo
bar

json_typeof(json)

jsonb_typeof(jsonb)

text

返回json最外层value的数据类型,可能的类型有

objectarraystringnumberboolean, 和null.

 select json_typeof('-123.4')  number

json_to_record(json)

jsonb_to_record(jsonb)

record  根据json对象创建一个record类型记录,所有的函数都返回record类型,所以必须使用as明确定义record的结构。  select * from json_to_record('{"a":1,"b":[1,2,3],"c":"bar"}') as x(a int, b text, d text);

a |    b    |   d 
---+---------+------
1 | [1,2,3] | NULL

json_to_recordset(json)

jsonb_to_recordset(jsonb)

setof record  根据json数组创建一个record类型记录,所有的函数都返回record类型,所以必须使用as明确定义record的结构。  select * from json_to_recordset('[{"a":1,"b":"foo"},{"a":"2","c":"bar"}]') as x(a int, b text);

a | b 
---+------
1 | foo
2 | NULL

json_strip_nulls(from_json json)

jsonb_strip_nulls(from_json jsonb)

json

jsonb

 返回json对象中所有非null的数据,其他的null保留。

select json_strip_nulls('[{"f1":1,"f2":null},2,null,3]');

  [{"f1":1},2,null,3]

jsonb_set(target jsonb, path text[],new_value jsonb[,create_missing boolean])

 jsonb  如果create_missing为true,则将在target的path处追加新的jsonb;如果为false,则替换path处的value。

select jsonb_set('[{"f1":1,"f2":null},2,null,3]', '{0,f1}','[2,3,4]', false);

select jsonb_set('[{"f1":1,"f2":null},2]', '{0,f3}','[2,3,4]');

[{"f1": [2, 3, 4], "f2": null}, 2, null, 3]

[{"f1": 1, "f2": null, "f3": [2, 3, 4]}, 2]

jsonb_insert(target jsonb, path text[],

new_value jsonb, [insert_after boolean])

 jsonb 如果insert_after是true,则在target的path后面插入新的value,否则在path之前插入。

select jsonb_insert('{"a": [0,1,2]}', '{a, 1}', '"new_value"');

select jsonb_insert('{"a": [0,1,2]}', '{a, 1}', '"new_value"', true);

{"a": [0, "new_value", 1, 2]}

{"a": [0, 1, "new_value", 2]}

 jsonb_pretty(from_json jsonb)  text  以缩进的格式更容易阅读的方式返回json对象  select jsonb_pretty('[{"f1":1,"f2":null},2,null,3]');


  { 
   "f1": 1,
   "f2": null
  }, 
  2,
  null,
  3
]

postgreSQL 玩转josnb (长期更新)的更多相关文章

  1. Ubuntu16.04系统美化、常用软件安装等,长期更新

    Ubuntu16.04系统美化.常用软件安装等,长期更新 IT之家啊 18-09-0915:00 因为我个人偏向于玩VPS.服务器之类的东西,所以一般我都是用CentOS.不过对于桌面版的Linux, ...

  2. Java代码优化(长期更新)

    前言 2016年3月修改,结合自己的工作和平时学习的体验重新谈一下为什么要进行代码优化.在修改之前,我的说法是这样的: 就像鲸鱼吃虾米一样,也许吃一个两个虾米对于鲸鱼来说作用不大,但是吃的虾米多了,鲸 ...

  3. iOS-工作经验+资料分享(长期更新)

    在此记录工作中的一些经验和技术资料 长期更新 欢迎各位业内朋友指正.交流技术上的问题 0.苹果开发联盟电话 4006 701855 1.轻易不用使用tableViewController,因为改变他自 ...

  4. Spring MVC & Boot & Cloud 技术教程汇总(长期更新)

    昨天我们发布了Java成神之路上的知识汇总,今天继续. Java成神之路技术整理(长期更新) 以下是Java技术栈微信公众号发布的关于 Spring/ Spring MVC/ Spring Boot/ ...

  5. [原创][FPGA]Quartus实用小技巧(长期更新)

    0. 简介 在使用Quartus软件时,经常会时不时的发现一些小技巧,本文的目的是总结所查阅或者发现到的小技巧,本文长期更新. 1. Quartus中的模板功能 最近在Quartus II的菜单里找到 ...

  6. python的多版本安装以及常见错误(长期更新)

    (此文长期更新)Python安装常见错误汇总 注:本教程以python3.6为基准 既然是总结安装过程中遇到的错误,就顺便记录一下我的安装过程好了. 先来列举一下安装python3.6过程中可能需要的 ...

  7. 项目中解决实际问题的代码片段-javascript方法,Vue方法(长期更新)

    总结项目用到的一些处理方法,用来解决数据处理的一些实际问题,所有方法都可以放在一个公共工具方法里面,实现不限ES5,ES6还有些Vue处理的方法. 都是项目中来的,有代码跟图片展示,长期更新. 1.获 ...

  8. 微信小程序常见问题集合(长期更新)

    最新更新: 新手跳坑系列:推荐阅读:<二十四>request:fail错误(含https解决方案)(真机预览问题 跳坑指南<七十>如何让微信小程序服务类目审核通过 跳坑六十九: ...

  9. [小细节,大BUG]记录一些小问题引起的大BUG(长期更新....)

    [小细节,大BUG] 6.问题描述:当从Plist文件加载数据,放入到tableView中展示时,有时有数据,有时又没有数据.这是为什么呢?相信很多大牛都想到了:我们一般将加载的数据,转换成模型,放入 ...

随机推荐

  1. Android 样式的开发(转)

    Android(2)    目录(?)[-] rectangle oval line ring layer-list篇 普通图片 bitmap标签 点九图片 nine-patch标签 color标签 ...

  2. 16个必须熟悉的linux服务器监控命令

    本原创文章属于<Linux大棚>博客. 博客地址为http://roclinux.cn. 文章作者为roc. == 原文:16 Linux Server Monitoring Comman ...

  3. dubbo-admin和dubbo-monitor的安装

    一.安装dubbo-admin 去这里 http://download.csdn.net/download/u013081610/10044744 下载dubbo-admin.war 部署dubbo- ...

  4. MySQL复制进阶

    Ⅰ.背景 搭建MySQL复制环境非常简单 你的系统是否也是像我之前那么搭建的呢? 那么,你的复制系统是否出现过以下的情况呢? 复制报错,例如:1062,1032 主从数据不一致 Ⅱ.真正高可靠复制环境 ...

  5. How to set spring boot active profiles with maven profiles

    In the previous post you could read about separate Spring Boot builds for a local development machin ...

  6. 你不知道的JavaScript--Item8 函数,方法,构造函数调用

    1.函数调用 Function绝对是JavaScript中的重中之重.在JavaScript中,Function承担了procedures, methods, constructors甚至是class ...

  7. 给xmpphp添加了几个常用的方法

    给xmpphp添加给了以下的常用方法: registerNewUser            //注册一个新用户 addRosterContact           //发送添加好友的请求 acce ...

  8. 学会这15点,让你分分钟拿下Redis数据库

    1.Redis简介 REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统.Redis是一个开源的使用ANSI ...

  9. ATOM系列之-atom报错"Cannot load the system dictionary for zh-CN"

    atom报错"Cannot load the system dictionary for zh-CN" 想必很多人(程序猿&程序媛)都和我一样,喜欢的这款很拉风的代码编辑器 ...

  10. Math.pow();Math.sqrt();

    //Math.pow(a,b)功能是a的b次方 (int)Math.sqrt(n):先对n开方,然后转成int类型//例如,(int)Math.sqrt(2)=(int)1.414=1 Math.ab ...