【问题标题】:Jolt Transformation of JSON with mutual exclusion具有互斥的 JSON 的颠簸转换
【发布时间】:2019-04-23 16:34:43
【问题描述】:

我正在尝试使用 JOLT 规范转换输入 JSON。我的输入有一个响应元素,它可以有一个文本(案例 1)值或一个 JSON 元素(案例 2),如下所示

JOLT 规格:

[
  {
    "operation": "shift",
    "spec": {
      "@(1,status)": {
        "@(2,output)": {
          "response": "statusMessage"
        },
        "TERMINATED": {
          "@(2,status)": "statusMessage"
        },
        "FAILED": {
          "@(2,response)": "statusMessage"
        },
        "COMPLETED": {
          "@(2,status)": "statusMessage"
        }
      },
      "status": "status"
    }
  }
]

输入(非 JSON 响应元素)。 ----案例一

{
  "createTime": 1555623377858,
  "updateTime": 1555623378681,
  "status": "FAILED",
  "output": {
    "response": "Connection error."
  }
}

输入(JSON 响应元素)。 ----案例2

{
  "createTime": 1555623377858,
  "updateTime": 1555623378681,
  "status": "FAILED",
  "output": {
    "response": {
      "headers": {
        "ETag": [
          "W/\"5-fy9qFc+NorJ+Wkr0e1jnrXHAs9k\""
        ],
        "Connection": [
          "keep-alive"
        ],
        "Content-Length": [
          "5"
        ],
        "Date": [
          "Thu, 18 Apr 2019 21:36:18 GMT"
        ],
        "Content-Type": [
          "text/html; charset=utf-8"
        ],
        "X-Powered-By": [
          "Express"
        ]
      },
      "reasonPhrase": "Internal Server Error",
      "body": "Error",
      "statusCode": 500
    }
  }
}

如果我想在响应包含 JSON 元素的情况下将“reasonPhrase”分配给 statusMessage,如何指定 JOLT 规范?

我的输出应该如下所示

Case 1 response should look like.
{
  "statusMessage" : "Connection Error",
  "status" : "FAILED"
}

Case 2 response should look like this.
{
  "statusMessage" : "Internal Server Error",
  "status" : "FAILED"
}

【问题讨论】:

    标签: json transformation jolt


    【解决方案1】:

    我认为我可以使用以下任一规格:

    1)

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "statusMessage": "@(1,output.response)"
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "statusMessage": "@(1,output.response.reasonPhrase)"
        }
      },
      {
        "operation": "shift",
        "spec": {
          "status": "status",
          "statusMessage": "statusMessage"
        }
      }
    ]
    

    链中的第一个规范将响应值存储到statusMessage 字段中。第二个将用嵌套的reasonPhrase 值(如果存在)覆盖statusMessage 字段。链中的最后一个规范只保留 statusstatusMessage 字段。

    2)

    [
      {
        "operation": "shift",
        "spec": {
          "output": {
            "response": {
              "@(1,response)": "statusMessage[]",
              "headers": {
                "@(1,reasonPhrase)": "statusMessage[]"
              }
            }
          },
          "status": "status"
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "statusMessage": "=lastElement(@(1,statusMessage))"
        }
      }
    ]
    

    它创建一个名为statusMessage 的数组,如果response 是一个字符串,则数组中将有一个元素,如果它是嵌套的,则数组中将有两个元素,第二个元素是所需的状态消息。所以第二个规范用数组中的最后一个元素覆盖了statusMessage 字段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-03
      • 2021-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多