根据 URL,我需要找到uri
类似内容并将fromTest123.elb.us-east-1.amazonaws.com
更改为connectionId
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 文件
{
"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 脚本:
- 打开文件
ex.json
并调用打开的文件json_file
- 将 JSON 读入名为的 python 字典中
data
- 循环遍历文档中的路径(例如,
/code123
) - 循环遍历每个路径的方法(例如,
post
) - 确定
uri
该元素中的字段是否包含目标字符串;find()
如果没有找到该字符串将返回-1。 - 如果
uri
给定的 atkey
包含您要查找的字符串,它将用connectionId
您想要的值覆盖该字段 - 循环完成后,它会将(可能修改的)JSON 打印到标准输出。