Có lẽ có một cách tốt hơn để làm điều này với thứ gì đó như jq , nhưng tôi chưa bao giờ thành thạo công cụ đó. Đối với tôi, tôi sẽ sử dụng Python cho việc này. Đưa ra tài liệu JSON được cập nhật của bạn {
"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"
}
}
}
}
}
}
}
Chạy tập lệnh Python này (với ví dụ trên có tên là 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))
Cho tôi kết quả sau khi trường connectionId của mục nhập đầu tiên đã thay đổi {
"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"
}
}
}
}
}
}
}
Kịch bản Python - Mở tệp
ex.json và gọi tệp đang mở đó là json_file - Đọc JSON vào từ điển python có tên
data - Lặp lại các đường dẫn trong tài liệu (e. g. ,
/code123 ) - Lặp lại các phương thức cho từng đường dẫn (e. g. ,
#!/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))
0) - Xác định xem trường
#!/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))
1 trong phần tử đó có chứa chuỗi đích hay không; - Nếu
#!/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))
1 tại #!/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))
4 đã cho chứa chuỗi bạn đang tìm kiếm, nó sẽ ghi đè lên trường connectionId với giá trị bạn muốn - Khi vòng lặp hoàn tất, nó sẽ in JSON (có khả năng được sửa đổi) thành đầu ra tiêu chuẩn
Làm cách nào để thay thế giá trị khóa trong JSON bằng Python?
Thay thế nhiều khóa và giá trị của tệp JSON bằng Python . thay thế khóa EntityHandle bằng Tên; thay thế giá trị của EntityHandle từ số hex bằng 'sf_001', 'sf_002', 'sf_003' , v.v.; thay thế giá trị của loại LineString bằng Polygon ; thay thế hai dấu ngoặc vuông của tọa độ bằng ba dấu ngoặc vuông
Làm cách nào để cập nhật một giá trị trong tệp JSON?
Cách cập nhật giá trị thuộc tính của tệp đối tượng json e. g. ("trạng thái"="thành công") . Giải tuần tự hóa json Truy cập mã thông báo Cập nhật giá trị
Làm cách nào để chuyển đổi chuỗi trong JSON Python?
bạn có thể biến nó thành JSON trong Python bằng cách sử dụng json. hàm tải() . json. hàm loading() chấp nhận đầu vào là một chuỗi hợp lệ và chuyển đổi nó thành một từ điển Python.
Kết xuất JSON trong Python là gì?
Phương thức dump() được dùng khi các đối tượng Python phải được lưu trữ trong một tệp . Các bãi () được sử dụng khi các đối tượng được yêu cầu ở định dạng chuỗi và được sử dụng để phân tích cú pháp, in, v.v. Kết xuất () cần tên tệp json trong đó đầu ra phải được lưu trữ dưới dạng đối số. |