频繁操作Json嵌套数据PostgreSQL配合JSON操作工具类+sql
文章目录
- 1.工具类
- 2.依赖
- 3.sql
本文档只是为了留档方便以后工作运维,或者给同事分享文档内容比较简陋命令也不是特别全,不适合小白观看,如有不懂可以私信,上班期间都是在得
背景:因为频繁操作json嵌套数据 PostgreSQL得JSON操作记录(本来还有一份MongoDB得,但是因为电脑卡死机文档没保存下来,气的我已经摔键盘了,后续重新写在进行补充)
1.工具类
package com.xhao.PostgreSQL;import com.alibaba.fastjson2.JSON;
import com.jayway.jsonpath.JsonPath;
import org.springframework.util.ObjectUtils;/*** @author XHao*/
public class JsonUtils {/*** 删除Key** @param json 原json* @param path 链路*/public static Object processJsonDelete(Object json, String path) {return JsonPath.parse(JSON.parse(json.toString())).delete(path).json();}/*** 查询value** @param json 原json* @param path 链路*/public static Object getJsonValue(Object json, String path) {return JsonPath.parse(JSON.parse(json.toString())).read(path).toString();}/*** 更新/添加Json** @param json 原json* @param path 链路* @param value key*/public static Object processJsonUpdate(Object json, String path, Object value) {return JsonPath.parse(JSON.parse(json.toString())).set(path, value).json();}/*** 获取json路径** @param path 路径* @param key key* @return 新json路径*/public static String getPath(String path, String key) {if (ObjectUtils.isEmpty(key)) {return getPath(path);} else if (key.matches("\\d+")) {return getPath(path) + "[" + key + "]";} else {return getPath(path) + "." + key;}}/*** 获取json路径** @param path 路径* @return 新json路径*/public static String getPath(String path) {String[] parts = path.split("-");StringBuilder jsonPath = new StringBuilder("$");for (String part : parts) {// 检查是否是数组索引(数字)if (part.matches("\\d+")) {jsonPath.append("[").append(part).append("]");} else {jsonPath.append(".").append(part);}}return jsonPath.toString();}public static void main(String[] args) {String str = "option-series-0-emphasis-textStyle";String str1 = null;System.err.println(getPath(str, str1));System.err.println(getPath(str));}
}
2.依赖
<!-- Hibernate 类型扩展 --><dependency><groupId>com.vladmihalcea</groupId><artifactId>hibernate-types-52</artifactId><version>2.14.0</version></dependency><!-- JsonPath --><dependency><groupId>com.jayway.jsonpath</groupId><artifactId>json-path</artifactId><version>2.7.0</version></dependency><!-- PostgreSQL 驱动 --><dependency><groupId>org.postgresql</groupId><artifactId>postgresql</artifactId></dependency>
3.sql
//查询
SELECT json->'option'->'xAxis'->0->>'show' AS show_value
FROM "public".test
WHERE id = 1;//修改
UPDATE "public".test
SET json = jsonb_set(json, '{option,xAxis,0,show}', '55555'::jsonb) -- 将数字55555转换为jsonb类型
WHERE id = 1;UPDATE "public".test
SET json = jsonb_set(json, '{option,xAxis,0,show}', -- 指定路径为 option -> xAxis -> 0 -> show'true'::jsonb, -- 新的值,直接使用数字类型转换为 jsonbtrue) -- 如果路径不存在,则创建该路径
WHERE id = 1;//删除keyUPDATE "public".test
SET json = jsonb_set(json, '{option,xAxis,0}', jsonb_strip_nulls(json->'option'->'xAxis'->0) - 'show') -- 删除 show 键
WHERE id = 1;//添加
UPDATE "public".test
SET json = jsonb_set(json, '{option,xAxis,0,show}', -- 指定路径为 option -> xAxis -> 0 -> showtrue::jsonb, -- 新的值,必须转换为 jsonb 类型true) -- 如果路径不存在,则创建该路径
WHERE id = 1;
因为电脑卡死机导致调研文档得MongoDB得demo没了,我也很苦恼后续重新写的话在更新
如果点赞多,评论多会更新详细教程,待补充。