Многоуровневый JSON и Python, как правильно добавить данные
есть вот такой json
{
"log": {
"access": "/var/log/xray/access.log",
"error": "/var/log/xray/error.log",
"loglevel": "info",
"dnsLog": false,
"maskAddress": ""
},
"routing": {
"rules": [],
"domainStrategy": "AsIs"
},
"inbounds": [
{
"port": 443,
"protocol": "vless",
"tag": "vless_tls",
"settings": {
"clients": [
{
"id": "XXXXX",
"email": "testuser0@myserver",
"flow": "xtls-rprx-vision"
},
{
"id": "YYYYY",
"email": "testuser1@myserver",
"flow": "xtls-rprx-vision"
},
{
"id": "ZZZZZ",
"email": "testuser2@myserver",
"flow": "xtls-rprx-vision"
}
],
"decryption": "none"
},
"streamSettings": {
"network": "tcp",
"security": "reality",
"realitySettings": {
"show": false,
"dest": "www.zzzz.yy:443",
"xver": 0,
"serverNames": [
"www.zzzz.yy"
],
"privateKey": "YYYYYYYYY",
"minClientVer": "",
"maxClientVer": "",
"maxTimeDiff": 0,
"shortIds": [
"XXXXXXXXX"
]
}
},
"sniffing": {
"enabled": true,
"destOverride": [
"http",
"tls"
]
}
},
],
"outbounds": [
{
"protocol": "freedom",
"tag": "direct"
},
{
"protocol": "blackhole",
"tag": "block"
}
]
}
Мне нужно добавить в clients три записи для нового пользователя, делаю так
with open('~/config.json', mode='r') as file:
data = json.load(file)
new_record=({'settings': {'clients': [{'id': uuid, 'email': email_n, 'flow': 'xtls-rprx-vision'}]}})
data["inbounds"].append(new_record)
with open('~/config.json', mode='w') as file:
json.dump(data, file, indent=4)
В итоге три значения добавляются, но не туда, куда нужно. Как добавить информацию именно в clients ?
Ответы (1 шт):
Автор решения: strawdog
→ Ссылка
Записи добавляются не туда, потому что вы их не туда добавляете.
new_record={'id': uuid, 'email': email_n, 'flow': 'xtls-rprx-vision'}
data["inbounds"][0]["settings"]["clients"].append(new_record)