【发布时间】:2021-09-08 02:10:57
【问题描述】:
这是我第一次使用 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))"
}
}
]
如果有人可以在这里提供一些指导和解释,我将不胜感激。
【问题讨论】: