Awk
根據 JSON 中的模式查找和替換
根據 URL,我需要找到
uri
喜歡Test123.elb.us-east-1.amazonaws.com
並將connectionId
from更改hkl876
為xed763
例如:定位
Test999.elb.us-east-1.amazonaws.com
和更新connectionId
從hkl876
到klm812
這是文件的範例內容
"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 腳本:
- 打開文件
ex.json
並呼叫打開的文件json_file
- 將 JSON 讀入一個名為
data
- 循環遍歷文件中的路徑(例如,
/code123
)- 循環每個路徑的方法(例如,
post
)- 確定
uri
該元素中的欄位是否包含目標字元串;find()
如果未找到該字元串,將返回 -1。- 如果
uri
給定的key
包含您要查找的字元串,它會connectionId
用您想要的值覆蓋該欄位- 循環完成後,它將(可能修改的)JSON 列印到標準輸出。