Mazdajai
asked on
python retrieve specific json key value and assign to variables
I have a python snippet that dumps the json data from a command.
I am trying to retrieve specific key value and assign to variables but not returning anything. Any thoughts?
Raw data:
Code:
Output:
I am trying to retrieve specific key value and assign to variables but not returning anything. Any thoughts?
Raw data:
/usr/bin/sudo pm2 jlist
[{"pid":4832,"name":"hypernova_service","pm2_env":{"env_production":{"NODE_ENV":"production"},"exec_mode":"fork_mode","env":{"PM2_JSON_PROCESSING":"true","PM2_INTERACTOR_PROCESSING":"true","PM2_USAGE":"CLI","PM2_HOME":"/var/data/hypernova/pm2","SHELL":"/sbin/nologin","USER":"www","LOGNAME":"www","HOME":"/nonexistent","PATH":"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin","LANG":"en_US.UTF-8","NODE_ENV":"production","hypernova_service":"{}"},"treekill":true,"autorestart":true,"automation":true,"pmx":true,"vizion":true,"merge_logs":true,"max_memory_restart":157286400,"name":"hypernova_service","node_args":[],"pm_exec_path":"/usr/lib/node_modules/@var/hypernova/index.js","pm_cwd":"/usr/lib/node_modules/@var/hypernova","exec_interpreter":"node","instances":1,"pm_out_log_path":"/var/log/hypernova/hypernova_out.log","pm_err_log_path":"/var/log/hypernova/hypernova_err.log","pm_pid_path":"/var/run/hypernova/hypernova-0.pid","km_link":false,"NODE_APP_INSTANCE":0,"vizion_running":false,"PM2_JSON_PROCESSING":"true","PM2_INTERACTOR_PROCESSING":"true","PM2_USAGE":"CLI","PM2_HOME":"/var/data/hypernova/pm2","SHELL":"/sbin/nologin","USER":"www","LOGNAME":"www","HOME":"/nonexistent","PATH":"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin","LANG":"en_US.UTF-8","NODE_ENV":"production","hypernova_service":"{}","status":"online","pm_uptime":1493747358783,"axm_actions":[],"axm_monitor":{"Loop delay":{"alert":{},"agg_type":"avg","value":"1.4ms"}},"axm_options":{"default_actions":true,"transactions":false,"http":false,"http_latency":200,"http_code":500,"ignore_routes":[],"profiling":true,"errors":true,"alert_enabled":true,"custom_probes":true,"network":false,"ports":false,"ignoreFilter":{"method":["OPTIONS"],"url":[]},"excludedHooks":[],"module_conf":{},"module_name":"hypernova_service","module_version":"2.4.0","pmx_version":"1.1.0","error":true},"axm_dynamic":{},"created_at":1493742698514,"pm_id":0,"restart_time":252,"unstable_restarts":0,"versioning":null,"node_version":"6.9.4","exit_code":1},"pm_id":0,"monit":{"memory":46551040,"cpu":0}}]
Code:
#!/usr/bin/python
import json
import subprocess
try:
cmd = '/usr/bin/sudo pm2 jlist'
except:
exit(1)
data = subprocess.check_output(cmd, shell=True)
jlists = json.loads(data)
print type(jlists)
for k, v in enumerate(jlists):
if (k == 'pid') or k == 'memory()':
print k,v
break
Output:
$ ./pm2.py
<type 'list'>
ASKER
Traceback (most recent call last):
File "./pm2_mem.py", line 21, in <module>
for k, v in jlists.items():
AttributeError: 'list' object has no attribute 'items'
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Open in new window