Awk

根據 JSON 中的模式查找和替換

  • August 15, 2020

根據 URL,我需要找到uri喜歡Test123.elb.us-east-1.amazonaws.com並將connectionIdfrom更改hkl876xed763

例如:定位Test999.elb.us-east-1.amazonaws.com和更新connectionIdhkl876klm812

這是文件的範例內容

  "x-amazon-apigateway-integration": {
     "uri": "http://Test123.elb.us-east-1.amazonaws.com:8765/emote",
     "responses": {
       "200": {
         "statusCode": "200",
         ......
         ......

     "connectionType": "VPC_LINK",
     "connectionId": "hkl876",
     "httpMethod": "POST",
     "type": "http"
   }
 },
   "x-amazon-apigateway-integration": {
     "uri": "http://Test999.elb.us-east-1.amazonaws.com:4567/authcode/v1/remote",
     "responses": {
       "200": {
         "statusCode": "200",
         ......
         ......

     "connectionType": "VPC_LINK",
     "connectionId": "hkl876",
     "httpMethod": "PUT",
     "type": "http"
   }

感謝您的建議。

當我在完整的 json 文件上嘗試此解決方案時,出現以下錯誤消息

Traceback (most recent call last):
 File "sample.py", line 16, in <module>
   if data[key]['uri'].find("test123.elb.us-east-1.amazonaws.com") > 0:
TypeError: string indices must be integers

這是一條記錄的完整招搖文件

{
 "swagger": "2.0",
 "info": {
   "version": "2019-02-19T19:13:11Z"
 },
 "host": "abc.com",
 "schemes": [
   "http"
 ],
 "paths": {
   "/code123": {
     "post": {
       "produces": [
         "application/json"
       ],
       "parameters": [
         {
           "name": "x-correlationid",
           "in": "header",
           "required": true,
           "type": "string"
         },
         {
           "name": "content-type",
           "in": "header",
           "required": true,
           "type": "string"
         }
       ],
       "responses": {
         "200": {
           "description": "200 response",
           "schema": {
             "$ref": "#/definitions/Empty"
           },
           "headers": {
             "Access-Control-Allow-Origin": {
               "type": "string"
             }
           }
         },
         "security": [
           {
             "RequestTokenAuthorizer": []
           },
           {
             "api_key": []
           }
         ],
         "x-amazon-apigateway-integration": {
           "uri": "http://test123.elb.us-east-1.amazonaws.com:2768/sample/code",
           "responses": {
             "200": {
               "statusCode": "200",
               "responseParameters": {
                 "method.response.header.Access-Control-Allow-Origin": "'*'"
               }
             },
             "requestParameters": {
               "integration.request.header.x-correlationid": "method.request.header.x-correlationid",
               "integration.request.header.x-brand": "method.request.header.x-brand"
             },
             "passthroughBehavior": "when_no_templates",
             "connectionType": "VPC_LINK",
             "connectionId": "xyz879",
             "httpMethod": "POST",
             "type": "http"
           }
         }
       }
     }
   }
 }
}

使用類似的東西可能有更好的方法jq,但我從未掌握過該工具。對我來說,我會為此使用 Python。鑑於您更新的 JSON 文件:

{
 "swagger": "2.0",
 "info": {
   "version": "2019-02-19T19:13:11Z"
 },
 "host": "abc.com",
 "schemes": [
   "http"
 ],
 "paths": {
   "/code123": {
     "post": {
       "produces": [
         "application/json"
       ],
       "parameters": [
         {
           "name": "x-correlationid",
           "in": "header",
           "required": true,
           "type": "string"
         },
         {
           "name": "content-type",
           "in": "header",
           "required": true,
           "type": "string"
         }
       ],
       "responses": {
         "200": {
           "description": "200 response",
           "schema": {
             "$ref": "#/definitions/Empty"
           },
           "headers": {
             "Access-Control-Allow-Origin": {
               "type": "string"
             }
           }
         },
         "security": [
           {
             "RequestTokenAuthorizer": []
           },
           {
             "api_key": []
           }
         ],
         "x-amazon-apigateway-integration": {
           "uri": "http://test123.elb.us-east-1.amazonaws.com:2768/sample/code",
           "responses": {
             "200": {
               "statusCode": "200",
               "responseParameters": {
                 "method.response.header.Access-Control-Allow-Origin": "'*'"
               }
             },
             "requestParameters": {
               "integration.request.header.x-correlationid": "method.request.header.x-correlationid",
               "integration.request.header.x-brand": "method.request.header.x-brand"
             },
             "passthroughBehavior": "when_no_templates",
             "connectionType": "VPC_LINK",
             "connectionId": "xyz879",
             "httpMethod": "POST",
             "type": "http"
           }
         }
       }
     }
   }
 }
}

執行這個 Python 腳本(上面的範例名為ex.json):

#!/usr/bin/env python3

import json

with open('ex.json') as json_file:
   data = json.load(json_file)

   for path in data['paths']:
       for method in data['paths'][path]:
               if data['paths'][path][method]['responses']['x-amazon-apigateway-integration']['uri'].find("test123.elb.us-east-1.amazonaws.com") > 0:
                   data['paths'][path][method]['responses']['x-amazon-apigateway-integration']['responses']['connectionId'] = 'xed763'

   print(json.dumps(data, indent=4))

獲取以下輸出,其中connectionId第一個條目的欄位已更改。

{
   "swagger": "2.0",
   "info": {
       "version": "2019-02-19T19:13:11Z"
   },
   "host": "abc.com",
   "schemes": [
       "http"
   ],
   "paths": {
       "/code123": {
           "post": {
               "produces": [
                   "application/json"
               ],
               "parameters": [
                   {
                       "name": "x-correlationid",
                       "in": "header",
                       "required": true,
                       "type": "string"
                   },
                   {
                       "name": "content-type",
                       "in": "header",
                       "required": true,
                       "type": "string"
                   }
               ],
               "responses": {
                   "200": {
                       "description": "200 response",
                       "schema": {
                           "$ref": "#/definitions/Empty"
                       },
                       "headers": {
                           "Access-Control-Allow-Origin": {
                               "type": "string"
                           }
                       }
                   },
                   "security": [
                       {
                           "RequestTokenAuthorizer": []
                       },
                       {
                           "api_key": []
                       }
                   ],
                   "x-amazon-apigateway-integration": {
                       "uri": "http://test123.elb.us-east-1.amazonaws.com:2768/sample/code",
                       "responses": {
                           "200": {
                               "statusCode": "200",
                               "responseParameters": {
                                   "method.response.header.Access-Control-Allow-Origin": "'*'"
                               }
                           },
                           "requestParameters": {
                               "integration.request.header.x-correlationid": "method.request.header.x-correlationid",
                               "integration.request.header.x-brand": "method.request.header.x-brand"
                           },
                           "passthroughBehavior": "when_no_templates",
                           "connectionType": "VPC_LINK",
                           "connectionId": "xed763",
                           "httpMethod": "POST",
                           "type": "http"
                       }
                   }
               }
           }
       }
   }
}

Python 腳本:

  1. 打開文件ex.json並呼叫打開的文件json_file
  2. 將 JSON 讀入一個名為data
  3. 循環遍歷文件中的路徑(例如,/code123
  4. 循環每個路徑的方法(例如,post
  5. 確定uri該元素中的欄位是否包含目標字元串;find()如果未找到該字元串,將返回 -1。
  6. 如果uri給定的key包含您要查找的字元串,它會connectionId用您想要的值覆蓋該欄位
  7. 循環完成後,它將(可能修改的)JSON 列印到標準輸出。

引用自:https://unix.stackexchange.com/questions/604652