简体   繁体   English

相互排斥的JSON颠簸转换

[英]Jolt Transformation of JSON with mutual exclusion

I am trying to transform input JSON using JOLT spec. 我正在尝试使用JOLT规范转换输入JSON。 My input has a response element which can have a single text (Case 1) value OR a JSON element (Case 2) like below 我的输入包含一个response元素,该元素可以具有单个文本(案例1)值,也可以具有JSON元素(案例2),如下所示

JOLT Spec: 规格

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

Input (Non-JSON response element) . 输入(非JSON响应元素)。 ----Case 1 - - 情况1

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

Input (JSON response element) . 输入(JSON响应元素)。 ----Case 2 ----案例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
    }
  }
}

How do I specify JOLT spec if I want to assign "reasonPhrase" to statusMessage in the case where the response has JSON element? 如果在响应具有JSON元素的情况下要向statusMessage分配“ reasonPhrase”,如何指定JOLT规范?

My output should look like below 我的输出应如下所示

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

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

I think I got it to work with either of the following specs: 我认为我可以使用以下任一规范:

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

The first spec in the chain will store the response value into the statusMessage field. 链中的第一个规范会将响应值存储到statusMessage字段中。 The second will overwrite the statusMessage field with the nested reasonPhrase value (if it exists). 第二个将使用嵌套的reasonPhrase值(如果存在)覆盖statusMessage字段。 The last spec in the chain just keeps the status and statusMessage fields. 链中的最后一个规范仅保留statusstatusMessage字段。

2) 2)

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

It creates an array called statusMessage , if response is a string there will be one element in the array, if it is nested there will be two elements in the array and the second one is the desired status message. 它创建了一个名为statusMessage的数组,如果response是一个字符串,则该数组中将有一个元素,如果嵌套,则该数组中将有两个元素,第二个是所需的状态消息。 So the second spec overwrites the statusMessage field with the last element in its array. 因此,第二个规范使用其数组中的最后一个元素覆盖了statusMessage字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM