首页 > 解决方案 > 用于解析和转换 JSON 数组的 Jolt 规范

问题描述

这是我第一次使用 Jolt,我对它可以执行的转换感到惊讶。我在网上关注了文档和一些帖子。

但是,我仍然面临以下挑战:

我有以下输入

{
  "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))"
    }
  }    
]

如果有人可以在这里提供一些指导和解释,我将不胜感激。

标签: jsonjolt

解决方案


您可以使用以下连续规格

[
  {
    "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用于数组的BodyHeaders并且Host应该根据每个需要单独处理它们,并且它们最终templateConfig.commonClientConfig.item[]在最后一个班次规范中的公共节点()下累积。

推荐阅读