JSONのパターンに基づいて検索と置換

JSONのパターンに基づいて検索と置換

URLに基​​づいて、uri次のようなものを見つけてTest123.elb.us-east-1.amazonaws.comからに変更する必要があります。connectionIdhkl876xed763

例: から まで探して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

これは1つのレコードの完全なSwaggerファイルです。

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

答え1

同様にこれを行うより良い方法があるかもしれませんが、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をPython辞書に読み込みます。data
  3. 文書のパスを繰り返します(例/code123:)。
  4. 各パスを繰り返す方法(例post:)
  5. uriこの要素のフィールドにターゲット文字列が含まれていることを確認してください。find()文字列がない場合は-1を返します。
  6. uri与えられたatにkey探している文字列が含まれている場合は、connectionId必要な値でフィールドを上書きします。
  7. ループが完了したら(修正できます)、JSONを標準出力に印刷します。

関連情報