Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
JSON parsing (nested)
#1
RAW JSON below

How would I output 'mem_free' value under the nested JSON - only if the 'hostname': 'hcb005sp01p.mydoman.com:8091'



{'name': 'default', 'nodes': [{'systemStats': {'cpu_utilization_rate': 14.55696202531646, 'swap_total': 4194299904, 'swap_used': 0, 'mem_total': 33686196224, 'mem_free': 13453971456}, 'interestingStats': {'cmd_get': 358, 'couch_docs_actual_disk_size': 21338653472, 'couch_docs_data_size': 14740539420, 'couch_spatial_data_size': 0, 'couch_spatial_disk_size': 0, 'couch_views_actual_disk_size': 0, 'couch_views_data_size': 0, 'curr_items': 11658632, 'curr_items_tot': 23321036, 'ep_bg_fetched': 1, 'get_hits': 357, 'mem_used': 16147424136, 'ops': 531, 'vb_active_num_non_resident': 3062552, 'vb_replica_curr_items': 11662404}, 'uptime': '8833959', 'memoryTotal': 33686196224, 'memoryFree': 13453971456, 'mcdMemoryReserved': 25700, 'mcdMemoryAllocated': 25700, 'couchApiBase': 'http://hcb001sp01p.mydoman.com:8092/', 'couchApiBaseHTTPS': 'https://hcb001sp01p.mydoman.com:18092/', 'clusterMembership': 'active', 'recoveryType': 'none', 'status': 'healthy', 'otpNode': '[email protected]', 'thisNode': True, 'hostname': 'hcb001sp01p.mydoman.com:8091', 'clusterCompatibility': 327685, 'version': '5.5.5-4521-enterprise', 'os': 'x86_64-unknown-linux-gnu', 'cpuCount': 8, 'ports': {'httpsMgmt': 18091, 'httpsCAPI': 18092, 'proxy': 11211, 'direct': 11210}, 'services': ['kv']}, {'systemStats': {'cpu_utilization_rate': 12.83354510800508, 'swap_total': 4194299904, 'swap_used': 0, 'mem_total': 33686196224, 'mem_free': 13612580864}, 'interestingStats': {'cmd_get': 2218, 'couch_docs_actual_disk_size': 20854504154, 'couch_docs_data_size': 14665063183, 'couch_spatial_data_size': 0, 'couch_spatial_disk_size': 0, 'couch_views_actual_disk_size': 0, 'couch_views_data_size': 0, 'curr_items': 11636232, 'curr_items_tot': 23261744, 'ep_bg_fetched': 0, 'get_hits': 2216, 'mem_used': 16119825024, 'ops': 2381, 'vb_active_num_non_resident': 2793049, 'vb_replica_curr_items': 11625512}, 'uptime': '8833659', 'memoryTotal': 33686196224, 'memoryFree': 13612580864, 'mcdMemoryReserved': 25700, 'mcdMemoryAllocated': 25700, 'couchApiBase': 'http://hcb002sp01p.mydoman.com:8092/', 'couchApiBaseHTTPS': 'https://hcb002sp01p.mydoman.com:18092/', 'clusterMembership': 'active', 'recoveryType': 'none', 'status': 'healthy', 'otpNode': '[email protected]', 'hostname': 'hcb002sp01p.mydoman.com:8091', 'clusterCompatibility': 327685, 'version': '5.5.5-4521-enterprise', 'os': 'x86_64-unknown-linux-gnu', 'cpuCount': 8, 'ports': {'httpsMgmt': 18091, 'httpsCAPI': 18092, 'proxy': 11211, 'direct': 11210}, 'services': ['kv']}, {'systemStats': {'cpu_utilization_rate': 11.13924050632911, 'swap_total': 4194299904, 'swap_used': 0, 'mem_total': 33686196224, 'mem_free': 13651996672}, 'interestingStats': {'cmd_get': 316, 'couch_docs_actual_disk_size': 20717103568, 'couch_docs_data_size': 14665769026, 'couch_spatial_data_size': 0, 'couch_spatial_disk_size': 0, 'couch_views_actual_disk_size': 0, 'couch_views_data_size': 0, 'curr_items': 11625809, 'curr_items_tot': 23258584, 'ep_bg_fetched': 2, 'get_hits': 316, 'mem_used': 16172765344, 'ops': 475, 'vb_active_num_non_resident': 2677239, 'vb_replica_curr_items': 11632775}, 'uptime': '8833294', 'memoryTotal': 33686196224, 'memoryFree': 13651996672, 'mcdMemoryReserved': 25700, 'mcdMemoryAllocated': 25700, 'couchApiBase': 'http://hcb003sp01p.mydoman.com:8092/', 'couchApiBaseHTTPS': 'https://hcb003sp01p.mydoman.com:18092/', 'clusterMembership': 'active', 'recoveryType': 'none', 'status': 'healthy', 'otpNode': '[email protected]', 'hostname': 'hcb003sp01p.mydoman.com:8091', 'clusterCompatibility': 327685, 'version': '5.5.5-4521-enterprise', 'os': 'x86_64-unknown-linux-gnu', 'cpuCount': 8, 'ports': {'httpsMgmt': 18091, 'httpsCAPI': 18092, 'proxy': 11211, 'direct': 11210}, 'services': ['kv']}, {'systemStats': {'cpu_utilization_rate': 22.89308176100629, 'swap_total': 4194299904, 'swap_used': 119382016, 'mem_total': 68045934592, 'mem_free': 28261691392}, 'interestingStats': {}, 'uptime': '8831833', 'memoryTotal': 68045934592, 'memoryFree': 28261691392, 'mcdMemoryReserved': 51914, 'mcdMemoryAllocated': 51914, 'couchApiBase': 'http://hcb004sp01p.mydoman.com:8092/', 'couchApiBaseHTTPS': 'https://hcb004sp01p.mydoman.com:18092/', 'clusterMembership': 'active', 'recoveryType': 'none', 'status': 'healthy', 'otpNode': '[email protected]', 'hostname': 'hcb004sp01p.mydoman.com:8091', 'clusterCompatibility': 327685, 'version': '5.5.5-4521-enterprise', 'os': 'x86_64-unknown-linux-gnu', 'cpuCount': 8, 'ports': {'httpsMgmt': 18091, 'httpsCAPI': 18092, 'proxy': 11211, 'direct': 11210}, 'services': ['index', 'n1ql']}, {'systemStats': {'cpu_utilization_rate': 24.46675031367629, 'swap_total': 4194299904, 'swap_used': 822464512, 'mem_total': 68045934592, 'mem_free': 28116471808}, 'interestingStats': {}, 'uptime': '8832347', 'memoryTotal': 68045934592, 'memoryFree': 28116471808, 'mcdMemoryReserved': 51914, 'mcdMemoryAllocated': 51914, 'couchApiBase': 'http://hcb005sp01p.mydoman.com:8092/', 'couchApiBaseHTTPS': 'https://hcb005sp01p.mydoman.com:18092/', 'clusterMembership': 'active', 'recoveryType': 'none', 'status': 'healthy', 'otpNode': '[email protected]', 'hostname': 'hcb005sp01p.mydoman.com:8091', 'clusterCompatibility': 327685, 'version': '5.5.5-4521-enterprise', 'os': 'x86_64-unknown-linux-gnu', 'cpuCount': 8, 'ports': {'httpsMgmt': 18091, 'httpsCAPI': 18092, 'proxy': 11211, 'direct': 11210}, 'services': ['index', 'n1ql']}], 'buckets': {'uri': '/pools/default/buckets?v=23797127&uuid=28041ac652ca5dde6a24b9e173023577', 'terseBucketsBase': '/pools/default/b/', 'terseStreamingBucketsBase': '/pools/default/bs/'}, 'remoteClusters': {'uri': '/pools/default/remoteClusters?uuid=28041ac652ca5dde6a24b9e173023577', 'validateURI': '/pools/default/remoteClusters?just_validate=1'}, 'alerts': [], 'alertsSilenceURL': '/controller/resetAlerts?uuid=28041ac652ca5dde6a24b9e173023577&token=0', 'controllers': {'addNode': {'uri': '/controller/addNodeV2?uuid=28041ac652ca5dde6a24b9e173023577'}, 'rebalance': {'uri': '/controller/rebalance?uuid=28041ac652ca5dde6a24b9e173023577'}, 'failOver': {'uri': '/controller/failOver?uuid=28041ac652ca5dde6a24b9e173023577'}, 'startGracefulFailover': {'uri': '/controller/startGracefulFailover?uuid=28041ac652ca5dde6a24b9e173023577'}, 'reAddNode': {'uri': '/controller/reAddNode?uuid=28041ac652ca5dde6a24b9e173023577'}, 'reFailOver': {'uri': '/controller/reFailOver?uuid=28041ac652ca5dde6a24b9e173023577'}, 'ejectNode': {'uri': '/controller/ejectNode?uuid=28041ac652ca5dde6a24b9e173023577'}, 'setRecoveryType': {'uri': '/controller/setRecoveryType?uuid=28041ac652ca5dde6a24b9e173023577'}, 'setAutoCompaction': {'uri': '/controller/setAutoCompaction?uuid=28041ac652ca5dde6a24b9e173023577', 'validateURI': '/controller/setAutoCompaction?just_validate=1'}, 'clusterLogsCollection': {'startURI': '/controller/startLogsCollection?uuid=28041ac652ca5dde6a24b9e173023577', 'cancelURI': '/controller/cancelLogsCollection?uuid=28041ac652ca5dde6a24b9e173023577'}, 'replication': {'createURI': '/controller/createReplication?uuid=28041ac652ca5dde6a24b9e173023577', 'validateURI': '/controller/createReplication?just_validate=1'}}, 'rebalanceStatus': 'none', 'rebalanceProgressUri': '/pools/default/rebalanceProgress', 'stopRebalanceUri': '/controller/stopRebalance?uuid=28041ac652ca5dde6a24b9e173023577', 'nodeStatusesUri': '/nodeStatuses', 'maxBucketCount': 10, 'autoCompactionSettings': {'parallelDBAndViewCompaction': False, 'databaseFragmentationThreshold': {'percentage': 30, 'size': 'undefined'}, 'viewFragmentationThreshold': {'percentage': 30, 'size': 'undefined'}, 'indexCompactionMode': 'circular', 'indexCircularCompaction': {'daysOfWeek': 'Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday', 'interval': {'fromHour': 0, 'toHour': 0, 'fromMinute': 0, 'toMinute': 0, 'abortOutside': False}}, 'indexFragmentationThreshold': {'percentage': 30}}, 'tasks': {'uri': '/pools/default/tasks?v=80526948'}, 'counters': {'rebalance_success': 43, 'rebalance_start': 43, 'failover': 10, 'failover_complete': 10, 'graceful_failover_success': 6, 'graceful_failover_start': 6, 'failover_node': 1}, 'indexStatusURI': '/indexStatus?v=95505507', 'checkPermissionsURI': '/pools/default/checkPermissions?v=W%2FAI8u1g8Q%2FJq23YgxYKx6MlXWA%3D', 'serverGroupsUri': '/pools/default/serverGroups?v=45636718', 'clusterName': 'cbsp01p', 'balanced': True, 'memoryQuota': 28672, 'indexMemoryQuota': 61440, 'ftsMemoryQuota': 2048, 'cbasMemoryQuota': 2815, 'eventingMemoryQuota': 256, 'storageTotals': {'ram': {'total': 101058588672, 'quotaTotal': 90194313216, 'quotaUsed': 78920024064, 'used': 100457213952, 'usedByData': 48440014504, 'quotaUsedPerNode': 26306674688, 'quotaTotalPerNode': 30064771072}, 'hdd': {'total': 332065001472, 'quotaTotal': 332065001472, 'used': 69159427149, 'usedByData': 62910261194, 'free': 262905574323}}}

ok this works

print (json_data['nodes'][0]['systemStats']['mem_total'])
print (json_data['nodes'][0]['hostname'])

But how to know how many elements there are from 0-> ??

print(len(json_data['nodes']))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Parsing large JSON josvink66 5 630 Jan-10-2024, 05:46 PM
Last Post: snippsat
  [split] Parse Nested JSON String in Python mmm07 4 1,521 Mar-28-2023, 06:07 PM
Last Post: snippsat
  Read nested data from JSON - Getting an error marlonbown 5 1,358 Nov-23-2022, 03:51 PM
Last Post: snippsat
  Parsing JSON pyStund 4 2,969 Jul-31-2022, 02:02 PM
Last Post: pyStund
  Convert nested sample json api data into csv in python shantanu97 3 2,808 May-21-2022, 01:30 PM
Last Post: deanhystad
  Json Parsing sshree43 5 1,780 May-04-2022, 09:21 PM
Last Post: snippsat
  json api data parsing elvis 0 924 Apr-21-2022, 11:59 PM
Last Post: elvis
  Convert python dataframe to nested json kat417 1 6,324 Mar-18-2022, 09:14 PM
Last Post: kat417
  string indices must be integers when parsing Json ilknurg 3 6,347 Mar-10-2022, 11:02 AM
Last Post: DeaD_EyE
  Problem with nested JSON Kalet 7 2,782 Dec-09-2021, 11:13 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020