⚡️Jolt -- 通过JSON配置来处理复杂数据转换的工具

ops/2025/3/14 17:04:56/

简介:一个能够通过JSON配置(特定的语法)来处理复杂数据转换的工具。
比如将API响应转换为内部系统所需的格式,或者处理来自不同来源的数据结构差异。例如,将嵌套的JSON结构扁平化,或者重命名字段,合并多个字段等。
名称含义

  • JSON + Bolt:
    JSON 是工具处理的数据格式。
    ​Bolt​(闪电)象征快速和高效,暗示 Jolt 能够像闪电一样快速完成 JSON 数据的转换。
    Bolt​的logo就是一个闪电⚡️标志

  • 在社区中,Jolt 的名称被解读为 ​JSON Transformation Language 的缩写,强调其作为 JSON 转换语言的定位。

  • 项目地址:https://github.com/bazaarvoice/jolt

  • 在线验证工具:https://jolt-demo.appspot.com/#inception

使用

1. 引入maven依赖

  • pom.xml
<dependency><groupId>com.bazaarvoice.jolt</groupId><artifactId>jolt-core</artifactId><version>0.1.7</version>
</dependency>
<dependency><groupId>com.bazaarvoice.jolt</groupId><artifactId>json-utils</artifactId><version>0.1.7</version>
</dependency>

json_26">2. 编写Jolt转换规范(json格式)

json">[{"operation": "shift","spec": {"id": "id","orderType": "type","orderProductList": {"*": {"sampleNumber": "orderProductList[&1].number","id": "orderProductList[&1].id"}},"createTime": "createTime"}},{"operation": "modify-overwrite-beta","spec": {"id": "=toString(@(1,id))","createTime": "=concat(@(1,createTime), ' 00:00:00')"}}
]

3. Java集成示例

  • DataTransformTool
import com.bazaarvoice.jolt.Chainr;
import com.bazaarvoice.jolt.JsonUtils;import java.util.List;/*** description: DataTransformTool <br>* date: 2025/3/13 11:47 <br>* author: Boo <br>* version: 1.0 <br>*/
public class DataTransformTool {public static String transform(String inputJson, String specPath) {List chainrSpecJSON = JsonUtils.classpathToList( specPath );Chainr chainr = Chainr.fromSpec( chainrSpecJSON );Object transformedOutput = chainr.transform( JsonUtils.jsonToObject(inputJson) );return JsonUtils.toJsonString( transformedOutput );}}
  • Test.java
public static main(String[] args) {String inputJson = "{" +"             \"id\": 123456789," +"             \"orderType\": \"orderTypecsss\"," +"             \"orderProductList\": [" +"                   {\"sampleNumber\": \"number001\", \"id\": 2}," +"                   {\"sampleNumber\": \"SKU456\", \"id\": 1}" +"               ]," +"             \"createTime\": \"2025-03-13\"" +"           }";System.out.println(DataTransformTool.transform(inputJson, "spec.json"));}
  • 运行结果
{"id": "123456789","type": "orderTypecsss","orderProductList": [{"number": "number001","id": 2},{"number": "SKU456","id": 1}],"createTime": "2025-03-13 00:00:00"
}

Jolt 转换规范的核心语法

AI整理,仔细辨别

核心操作类型

操作类型用途示例
shift字段映射/结构调整“oldField”: “newField”
modify-overwrite-beta数据修改(类型转换/计算)“field”: “=toUpper”
default设置默认值“field”: “defaultVal”
remove删除字段“fieldToRemove”: “”
sort字段排序无参数,直接使用

核心操作符及示例

1. ​shift

用途:字段重映射、结构调整。
语法:将输入路径映射到输出路径。
示例:

json">{"operation": "shift","spec": {"inputField": "output.parent.child",  // 简单映射"nested.input.value": "output.value", // 嵌套映射"array[*].id": "output.ids[]"         // 数组展开}
}
2. ​default

用途:设置默认值(字段不存在时生效)。
语法:

json">{"operation": "default","spec": {"output.role": "guest",      // 单值默认"output.tags": ["default"]  // 数组默认}
}
3. ​remove

用途:删除指定字段。
语法:

json">{"operation": "remove","spec": {"unusedField": "",          // 删除字段"nested.tempData": ""       // 删除嵌套字段}
}
4. ​sort

用途:按字母序排序对象键。
语法:

json">{ "operation": "sort" }
5. ​cardinality

用途:强制字段为单值或数组。
语法:

json">{"operation": "cardinality","spec": {"output.roles": "array",  // 强制为数组"output.name": "single"    // 强制为单值}
}
6. ​modify-overwrite-beta

用途:覆盖或修改字段值(需 Jolt 扩展库)。
语法:

json">{"operation": "modify-overwrite-beta","spec": {"output.score": "=toDouble(@(1,input.score))"  // 转换为浮点数}
}
7. ​modify-default-beta

用途:条件默认值(类似 default 但支持表达式)。
语法:

json">{"operation": "modify-default-beta","spec": {"output.status": "=if (isPresent(@(1,input.status))) then @(1,input.status) else 'pending'"}
}

路径语法规则

​点号.:表示嵌套字段,如 user.address.city。
​通配符*:匹配任意字段或数组元素:
users[].name:提取所有 users 元素的 name。
data.
.value:匹配 data 下的所有子字段的 value。
​数组索引[n]:定位数组的特定位置,如 items[0].id。
​转义\:若字段名包含 . 或 *,需转义,如 field\.with\.dots。

链式操作示例

json

json">[// 第一步:字段映射{"operation": "shift","spec": {"firstName": "user.name","age": "user.details.age"}},// 第二步:添加默认值{"operation": "default","spec": {"user.role": "guest","user.details.active": true}},// 第三步:删除冗余字段{"operation": "remove","spec": {"user.details.age": ""}}
]

输入:

{ "firstName": "John", "age": 30 }

输出:

{"user": {"name": "John","role": "guest","details": {"active": true}}
}

高级技巧

  1. 动态键名:使用 & 引用输入字段的值作为键:
json">{"operation": "shift","spec": {"userType": "output.&"  // 将 userType 的值作为键}
}

输入 { “userType”: “admin” } → 输出 { “output”: { “admin”: “admin” } }。

  1. ​条件逻辑:通过 modify 系列操作符实现复杂条件:
json">{"operation": "modify-overwrite-beta","spec": {"output.discount": "=if (@(1,price) > 100) then 0.1 else 0"}
}
  1. ​数组聚合:将多个字段合并为数组:
json">{"operation": "shift","spec": {"tags": "output.tags[]"}
}

注意事项

  • 大小写敏感:字段名和路径严格区分大小写。
  • ​路径不存在:若输入路径不存在,操作符会静默忽略。
  • 性能优化:复杂嵌套或通配符可能影响性能,尽量简化规则。

http://www.ppmy.cn/ops/165716.html

相关文章

vue3 中使用 Recorder 实现录音并上传,并用Go语言调取讯飞识别录音(Go语言)

录音并识别 效果图一、开启游览器录音权限二、前端代码三、Go代码,上传到讯飞识别录音返回到前端 效果图 recorder-core插件可以在网页中进行录音。录音文件(blob)并可以自定义上传&#xff0c;可以下载录音文件到本地,本文录音过程中会显示可视化波形&#xff0c;插件兼容PC端…

赛事|基于SprinBoot+vue的CSGO赛事管理系统(源码+数据库+文档)

CSGO赛事管理系统 目录 基于SprinBootvue的CSGO赛事管理系统 一、前言 二、系统设计 三、系统功能设计 1系统功能模块 2管理员功能模块 3参赛战队功能模块 4合作方功能模块 四、数据库设计 五、核心代码 六、论文参考 七、最新计算机毕设选题推荐 八、源码获取&…

OBJ文件生成PCD文件(python 实现)

代码实现 将 .obj 文件转换为 .pcd&#xff08;点云数据&#xff09; 代码文件。 import open3d as o3d# 加载 .obj 文件 mesh o3d.io.read_triangle_mesh("bunny.obj")# 检查是否成功加载 if not mesh.has_vertices():print("无法加载 .obj 文件&#xff0c…

Manus:成为AI Agent领域的标杆

一、引言 官网&#xff1a;Manus 随着人工智能技术的飞速发展&#xff0c;AI Agent&#xff08;智能体&#xff09;作为人工智能领域的重要分支&#xff0c;正逐渐从概念走向现实&#xff0c;并在各行各业展现出巨大的应用潜力。在众多AI Agent产品中&#xff0c;Manus以其独…

C# net deepseek RAG AI开发 全流程 介绍

deepseek本地部署教程及net开发对接 步骤详解&#xff1a;安装教程及net开发对接全流程介绍 DeepSeekRAG 中的 RAG&#xff0c;全称是 Retrieval-Augmented Generation&#xff08;检索增强生成&#xff09;&#xff0c;是一种结合外部知识库检索与大模型生成能力的技术架构。其…

2025年03月11日Github流行趋势

项目名称&#xff1a;pydoll 项目地址url&#xff1a;https://github.com/thalissonvs/pydoll项目语言&#xff1a;Python历史star数&#xff1a;1372今日star数&#xff1a;148项目维护者&#xff1a;thalissonvs, apps/github-actions, LucasAlvws, CaioWzy, Patolox项目简介…

PHP语言的开源贡献

PHP语言的开源贡献及其影响 引言 在互联网技术飞速发展的今天&#xff0c;开源软件已经成为了软件开发的重要组成部分。它不仅改变了我们开发和使用软件的方式&#xff0c;更在促进技术共享、推动创新和降低开发成本等方面发挥了重要作用。而在众多的开源项目中&#xff0c;P…

golang的Map

Map集合 概述 Map 是一种无序的键值对的集合。 Map 最重要的一点是通过 key 来快速检索数据&#xff0c;key 类似于索引&#xff0c;指向数据的值。 Map 是一种集合&#xff0c;所以我们可以像迭代数组和切片那样迭代它。不过&#xff0c;Map 是无序的&#xff0c;遍历 Map…