json - 用于解析和转换 JSON 数组的 Jolt 规范
问题描述
这是我第一次使用 Jolt,我对它可以执行的转换感到惊讶。我在网上关注了文档和一些帖子。
但是,我仍然面临以下挑战:
- 将新值连接到现有值(本例中为 url)
- 将连接值移动到正确的位置
- 如果输入 JSON 中不存在,则添加默认键值对
- 将 JSON 对象分配给正确的 JSON 数组。
我有以下输入
{
"Body": [
{
"username": "some-user"
},
{
"password": "*******"
}
],
"hostSource": "infos",
"Host": [
{
"HOST_NAME": "xyz.com"
},
{
"PORT": "9085"
}
],
"Headers": [
{
"Content-Type": "application/json"
}
]
}
我的预期输出是:
{
"templateConfig": {
"commonClientConfig ": {
"item": [
{
"name": "Main API - BASICAUTH",
"request": {
"auth": {
"type": "basic",
"basic": [
{
"key": "password",
"value": "*******",
"type": "string"
},
{
"key": "username",
"value": "some-user",
"type": "string"
}
]
},
"method": "GET",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"url": {
"raw": "xyz.com/v9085/some-default-value"
}
}
}
]
}
}
}
在参考了一些帖子和文档之后,我能够通过规范做到这一点:
[
{
"operation": "shift",
"spec": {
"Body": {
"*": {
"*": {
"$": "commonClientConfig.item.request.auth.basic[&2].key",
"@(1,&)": "commonClientConfig.item.request.auth.basic[&2].value"
}
}
},
"Headers": {
"*": {
"*": {
"$": "commonClientConfig.item.request.header[&2].key",
"@(1,&)": "commonClientConfig.item.request.header[&2].value"
}
}
},
"Host": {
"*": {
"*": {
"@(1,&)": "commonClientConfig.item.request.url.raw"
}
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"content": "=join('/', @(2,commonClientConfig.item.request.url.raw))"
}
}
]
如果有人可以在这里提供一些指导和解释,我将不胜感激。
解决方案
您可以使用以下连续规格
[
{
"operation": "shift",
"spec": {
"#Main API - BASICAUTH": "name",
"Body": {
"#basic": "request.auth.type",
"*": {
"*": {
"$": "request.auth.basic[&2].key",
"@": "request.auth.basic[&2].value",
"#string": "request.auth.basic[&2].type"
}
}
},
"Headers": {
"#GET": "request.method",
"*": {
"*": {
"$": "request.header[&2].key",
"@": "request.header[&2].value",
"#text": "request.header[&2].type"
}
}
},
"Host": {
"*": {
"*": {
"@": "request.url"
}
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"request": {
"url": "=join('/', @(2,request.url))"
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"request": {
"url": "=concat(@(1,url), /some-default-value)"
}
}
},
{
"operation": "shift",
"spec": {
"*": "&",
"request": {
"*": "&1.&",
"url": "&1.&.raw"
}
}
},
{
"operation": "shift",
"spec": {
"@": "templateConfig.commonClientConfig.item[]"
}
}
]
在哪里
join
函数用于通过组合数组的每个元素而不重复提及它们来导出连接字符串,而concat
仅用于处理默认字符串的单个连接。- 请注意,最里面的公共键是
request
用于数组的Body
,Headers
并且Host
应该根据每个需要单独处理它们,并且它们最终templateConfig.commonClientConfig.item[]
在最后一个班次规范中的公共节点()下累积。
推荐阅读
- laravel - Laravel 7 存储图像显示直接运行到浏览器 url 但无法处理我的代码
- android - VideoView 在类中的 setpath 的语法是什么?
- ffmpeg - 是否可以使用 MinGW 为 Windows 10 成功编译工作 ffmpeg 的临时修复?
- google-chrome-extension - 谷歌扩展 - 消息监听器不起作用
- sql - 运行总计未与 GrandTotal 对齐
- reactjs - 我的缓存 API 在 React PWA 中不起作用
- javascript - 如何在 for 循环中正确访问数组的元素?
- appium - 如何使用 appium 在隐身模式下启动 chrome 浏览器?
- r - R中的经验频率分布
- css - 画布 - 如何为画布内的图像设置 css