从 JSON 文件中获取特定值并将其传递到现有 YAML 文件中的特定字段
Getting specific values from a JSON file and put it through to specific fields in an existing YAML file
这是 JSON 文件,vip5.json
。
{
"App Name": "test",
"Email": "test@test.com",
"Employee ID": "abc",
"Load Balancing Method": "Ratio",
"Network": "CMN",
"Pool Member": "pucq",
"Pool Monitor": "tcp",
"Pool name": "pool",
"SSL": "Required",
"VIP Name": "vs"
}
这是 YAML 文件 test.yaml
。
---
server: pucl-k-030.company.com
partition: Common
nodes:
- host: 10.74.204.75
name: node-puex-spi-109
description: PUEX1
monitors:
- /Common/icmp
- host: 10.74.204.76
name: node-puex-spi-110
description: PUEX2
monitors:
- /Common/icmp
pool:
name: pool-puex-indexers
descriptions: PUEX Indexers pool
lb_method:
monitors:
- /Common/tcp
pool_members:
- node_name: node-puex-109
port: 9997
- node_name: node-puex-110
port: 9997
virtual_server:
name: vs-ng-puex-test-frwd
destination: 1.1.1.1
ip_protocol: udp
port: 999
type: performance-l4
profiles:
- name: fastL4
pool: pool-puex-indexers
我想获取这些值并将其添加到我的 YAML 文件的某些字段中。假设我想从 JSON 文件中的 "Load Balancing Method:" 中获取 Ratio
的值,并将其放入 [= YAML 文件中的 30=]。我怎么做?
我尝试读取 JOSN 文件并遍历该文件。但我不确定这是不是要走的路。
import json
import requests
import yaml
url = "http://127.0.0.1:5000/vip5.json"
r = requests.get(url)
json_file = json.loads(r.content)
print(json_file)
欢迎来到Python!在这种情况下,您有一堆键值数据,最好将两个文档读入 dict
可以轻松比较的对象。这是您描述的解决方案。
import json
import yaml
# Load files as dicts
with open('vip5.json', 'r') as f:
j = json.load(f)
with open('test.yaml', 'r') as f:
y = yaml.load(f)
# Assign JSON "Load Balancing Method" to YAML "lb_method".
y['pool']['lb_method'] = j['Load Balancing Method']
print(y)
您可以对此进行详细说明以构建您想要的特定映射。有帮助吗?
这是 JSON 文件,vip5.json
。
{
"App Name": "test",
"Email": "test@test.com",
"Employee ID": "abc",
"Load Balancing Method": "Ratio",
"Network": "CMN",
"Pool Member": "pucq",
"Pool Monitor": "tcp",
"Pool name": "pool",
"SSL": "Required",
"VIP Name": "vs"
}
这是 YAML 文件 test.yaml
。
---
server: pucl-k-030.company.com
partition: Common
nodes:
- host: 10.74.204.75
name: node-puex-spi-109
description: PUEX1
monitors:
- /Common/icmp
- host: 10.74.204.76
name: node-puex-spi-110
description: PUEX2
monitors:
- /Common/icmp
pool:
name: pool-puex-indexers
descriptions: PUEX Indexers pool
lb_method:
monitors:
- /Common/tcp
pool_members:
- node_name: node-puex-109
port: 9997
- node_name: node-puex-110
port: 9997
virtual_server:
name: vs-ng-puex-test-frwd
destination: 1.1.1.1
ip_protocol: udp
port: 999
type: performance-l4
profiles:
- name: fastL4
pool: pool-puex-indexers
我想获取这些值并将其添加到我的 YAML 文件的某些字段中。假设我想从 JSON 文件中的 "Load Balancing Method:" 中获取 Ratio
的值,并将其放入 [= YAML 文件中的 30=]。我怎么做?
我尝试读取 JOSN 文件并遍历该文件。但我不确定这是不是要走的路。
import json
import requests
import yaml
url = "http://127.0.0.1:5000/vip5.json"
r = requests.get(url)
json_file = json.loads(r.content)
print(json_file)
欢迎来到Python!在这种情况下,您有一堆键值数据,最好将两个文档读入 dict
可以轻松比较的对象。这是您描述的解决方案。
import json
import yaml
# Load files as dicts
with open('vip5.json', 'r') as f:
j = json.load(f)
with open('test.yaml', 'r') as f:
y = yaml.load(f)
# Assign JSON "Load Balancing Method" to YAML "lb_method".
y['pool']['lb_method'] = j['Load Balancing Method']
print(y)
您可以对此进行详细说明以构建您想要的特定映射。有帮助吗?