парсинг json

rust

Новичок
Пользователь
Авг 6, 2020
2
0
1
Всем привет,
Возникли проблемы с парсингом json

Name: zzz-mautic-test
Date: 2019-11-21T13:16:43+01:00
Status: Running
State: {'disk': {'root': {'usage': 107171840}}, 'cpu': {'usage': 0}, 'status_code': 103, 'processes': 24, 'memory': {'swap_usage_peak': 0, 'swap_usage': 0, 'usage_peak': 135307264, 'usage': 45236224}, 'pid': 6073, 'status': 'Running', 'network': {'lo': {'host_name': '', 'type': 'loopback', 'hwaddr': '', 'state': 'up', 'mtu': 65536, 'counters': {'packets_received': 2490, 'bytes_sent': 251472, 'bytes_received': 251472, 'packets_sent': 2490}, 'addresses': [{'netmask': '8', 'family': 'inet', 'scope': 'local', 'address': '127.0.0.1'}, {'address': '::1', 'netmask': '128', 'family': 'inet6', 'scope': 'local'}]}, 'eth0': {'state': 'up', 'mtu': 1500, 'counters': {'packets_sent': 4441, 'bytes_sent': 425876, 'bytes_received': 104981554, 'packets_received': 1432121}, 'addresses': [{'address': 'fd87:1cf1:9a65:0:216:3eff:feec:8db', 'netmask': '64', 'family': 'inet6', 'scope': 'global'}, {'address': 'fd97:522f:fa6:0:216:3eff:feec:8db', 'scope': 'global', 'netmask': '64', 'family': 'inet6'}, {'scope': 'global', 'family': 'inet6', 'netmask': '128', 'address': 'fd00:10:17:a0:f8f6:a9fa:ae2:257f'}, {'address': 'fd00:10:17:a0:216:3eff:feec:8db', 'scope': 'global', 'family': 'inet6', 'netmask': '64'}, {'address': 'fe80::216:3eff:feec:8db', 'netmask': '64', 'family': 'inet6', 'scope': 'link'}], 'host_name': '', 'type': 'broadcast', 'hwaddr': '00:16:3e:ec:08:db'}}}

Изначально речь шла о json файле с lxc контейнерами, из которых нужно извлечь имя, дату, статус для каждого.Из State: мне нужно извлечь 'cpu', 'memory' и из 'addresses' нужно извлечь все IP адреса. И вот с этим проблема, не получается из State: извлечь эти данные. Перебробовал уже наверно все но ничего не выходит. Буду очень благодарен за совет! Мой код пока который работает:

import json

with open ('sample-data.json') as f:
sampleData=json.load(f)

for item in sampleData:
print("Name: {}\nDate: {}\nStatus: {}\nState: {}\n".format(item['name'],item['created_at'],item['status'],item['state']))
 

Ketovx

Новичок
Пользователь
Авг 1, 2020
9
2
3
24
RF, KZN
Пришлите, что программа выводит в консоль.
 

rust

Новичок
Пользователь
Авг 6, 2020
2
0
1
Name: wiki-test
Date: 2019-10-22T13:51:14+02:00
Status: Running
State: {'network': {'lo': {'hwaddr': '', 'host_name': '', 'type': 'loopback', 'addresses': [{'address': '127.0.0.1', 'scope': 'local', 'family': 'inet', 'netmask': '8'}, {'family': 'inet6', 'netmask': '128', 'scope': 'local', 'address': '::1'}], 'counters': {'bytes_received': 0, 'bytes_sent': 0, 'packets_sent': 0, 'packets_received': 0}, 'mtu': 65536, 'state': 'up'}, 'eth0': {'host_name': '', 'type': 'broadcast', 'hwaddr': '00:16:3e:4a:ba:bb', 'state': 'up', 'mtu': 1500, 'counters': {'packets_sent': 50, 'bytes_received': 25083316, 'bytes_sent': 6555, 'packets_received': 188337}, 'addresses': [{'netmask': '20', 'family': 'inet', 'scope': 'global', 'address': '10.17.165.138'}, {'address': 'fd87:1cf1:9a65:0:216:3eff:fe4a:babb', 'scope': 'global', 'family': 'inet6', 'netmask': '64'}, {'scope': 'global', 'netmask': '64', 'family': 'inet6', 'address': 'fd97:522f:fa6:0:216:3eff:fe4a:babb'}, {'address': 'fd00:10:17:a0:216:3eff:fe4a:babb', 'netmask': '64', 'family': 'inet6', 'scope': 'global'}, {'netmask': '64', 'family': 'inet6', 'scope': 'link', 'address': 'fe80::216:3eff:fe4a:babb'}]}}, 'status': 'Running', 'status_code': 103, 'processes': 13, 'memory': {'usage': 1527808, 'usage_peak': 16437248, 'swap_usage': 0, 'swap_usage_peak': 0}, 'pid': 5653, 'cpu': {'usage': 0}, 'disk': {'root': {'usage': 877539328}}}

Name: zookeeper-ui
Date: 2020-03-11T13:38:13+01:00
Status: Running
State: {'memory': {'usage_peak': 779894784, 'usage': 698519552, 'swap_usage_peak': 0, 'swap_usage': 0}, 'pid': 1639, 'status_code': 103, 'processes': 96, 'status': 'Running', 'network': {'eth0': {'host_name': '', 'type': 'broadcast', 'hwaddr': '00:16:3e:b4:c5:db', 'mtu': 1500, 'state': 'up', 'addresses': [{'address': '10.17.164.94', 'scope': 'global', 'family': 'inet', 'netmask': '20'}, {'address': 'fd87:1cf1:9a65:0:216:3eff:feb4:c5db', 'scope': 'global', 'family': 'inet6', 'netmask': '64'}, {'family': 'inet6', 'netmask': '64', 'scope': 'global', 'address': 'fd97:522f:fa6:0:216:3eff:feb4:c5db'}, {'scope': 'global', 'netmask': '64', 'family': 'inet6', 'address': 'fd00:10:17:a0:216:3eff:feb4:c5db'}, {'address': 'fe80::216:3eff:feb4:c5db', 'family': 'inet6', 'netmask': '64', 'scope': 'link'}], 'counters': {'packets_sent': 352042, 'bytes_sent': 54721257, 'bytes_received': 633903929, 'packets_received': 499467}}, 'br-919c6a3ab91f': {'addresses': [{'address': '172.19.0.1', 'scope': 'global', 'family': 'inet', 'netmask': '16'}], 'counters': {'bytes_sent': 0, 'bytes_received': 0, 'packets_sent': 0, 'packets_received': 0}, 'mtu': 1500, 'state': 'up', 'hwaddr': '02:42:08:56:e2:cc', 'host_name': '', 'type': 'broadcast'}, 'lo': {'host_name': '', 'type': 'loopback', 'hwaddr': '', 'state': 'up', 'mtu': 65536, 'counters': {'packets_received': 583720, 'bytes_received': 158768485, 'bytes_sent': 158768485, 'packets_sent': 583720}, 'addresses': [{'address': '127.0.0.1', 'scope': 'local', 'netmask': '8', 'family': 'inet'}, {'address': '::1', 'family': 'inet6', 'netmask': '128', 'scope': 'local'}]}, 'docker0': {'host_name': '', 'type': 'broadcast', 'hwaddr': '02:42:80:b5:86:1e', 'state': 'up', 'mtu': 1500, 'counters': {'packets_sent': 42505, 'bytes_received': 1674213, 'bytes_sent': 111764457, 'packets_received': 30791}, 'addresses': [{'family': 'inet', 'netmask': '16', 'scope': 'global', 'address': '172.17.0.1'}, {'netmask': '64', 'family': 'inet6', 'scope': 'link', 'address': 'fe80::42:80ff:feb5:861e'}]}, 'veth91a7970': {'hwaddr': '46:fe:a8:4e:92:cd', 'host_name': '', 'type': 'broadcast', 'addresses': [{'netmask': '64', 'family': 'inet6', 'scope': 'link', 'address': 'fe80::44fe:a8ff:fe4e:92cd'}], 'counters': {'packets_sent': 125, 'bytes_sent': 19444, 'bytes_received': 21365, 'packets_received': 104}, 'mtu': 1500, 'state': 'up'}, 'br-3062d73d4ba8': {'hwaddr': '02:42:5f:d8:ad:7c', 'type': 'broadcast', 'host_name': '', 'counters': {'packets_received': 107, 'bytes_received': 20081, 'bytes_sent': 19325, 'packets_sent': 123}, 'addresses': [{'address': '172.18.0.1', 'scope': 'global', 'netmask': '16', 'family': 'inet'}, {'family': 'inet6', 'netmask': '64', 'scope': 'link', 'address': 'fe80::42:5fff:fed8:ad7c'}], 'state': 'up', 'mtu': 1500}}, 'cpu': {'usage': 0}, 'disk': {'root': {'usage': 27693711360}}}

Name: zzz-mautic-test
Date: 2019-11-21T13:16:43+01:00
Status: Running
State: {'disk': {'root': {'usage': 107171840}}, 'cpu': {'usage': 0}, 'status_code': 103, 'processes': 24, 'memory': {'swap_usage_peak': 0, 'swap_usage': 0, 'usage_peak': 135307264, 'usage': 45236224}, 'pid': 6073, 'status': 'Running', 'network': {'lo': {'host_name': '', 'type': 'loopback', 'hwaddr': '', 'state': 'up', 'mtu': 65536, 'counters': {'packets_received': 2490, 'bytes_sent': 251472, 'bytes_received': 251472, 'packets_sent': 2490}, 'addresses': [{'netmask': '8', 'family': 'inet', 'scope': 'local', 'address': '127.0.0.1'}, {'address': '::1', 'netmask': '128', 'family': 'inet6', 'scope': 'local'}]}, 'eth0': {'state': 'up', 'mtu': 1500, 'counters': {'packets_sent': 4441, 'bytes_sent': 425876, 'bytes_received': 104981554, 'packets_received': 1432121}, 'addresses': [{'address': 'fd87:1cf1:9a65:0:216:3eff:feec:8db', 'netmask': '64', 'family': 'inet6', 'scope': 'global'}, {'address': 'fd97:522f:fa6:0:216:3eff:feec:8db', 'scope': 'global', 'netmask': '64', 'family': 'inet6'}, {'scope': 'global', 'family': 'inet6', 'netmask': '128', 'address': 'fd00:10:17:a0:f8f6:a9fa:ae2:257f'}, {'address': 'fd00:10:17:a0:216:3eff:feec:8db', 'scope': 'global', 'family': 'inet6', 'netmask': '64'}, {'address': 'fe80::216:3eff:feec:8db', 'netmask': '64', 'family': 'inet6', 'scope': 'link'}], 'host_name': '', 'type': 'broadcast', 'hwaddr': '00:16:3e:ec:08:db'}}}

3 последних контейнера, их там в целом 20 гдето
 

Форум IT Специалистов