4m1nh4j1 4m1nh4j1 - 3 months ago 14
JSON Question

Convert a string to JSON

I would like to convert this string to a JSON dict:

{u'Processes': [[u'root', u'3606', u'0.0', u'0.2', u'76768', u'16664', u'?', u'Ss', u'20:40', u'0:01', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'4088', u'0.0', u'0.2', u'88544', u'20156', u'?', u'S', u'20:40', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'4090', u'0.0', u'0.2', u'88552', u'20140', u'?', u'S', u'20:40', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'4097', u'0.0', u'0.2', u'88552', u'20112', u'?', u'S', u'20:40', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'4110', u'0.0', u'0.2', u'88548', u'20160', u'?', u'S', u'20:40', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0']], u'Titles': [u'USER', u'PID', u'%CPU', u'%MEM', u'VSZ', u'RSS', u'TTY', u'STAT', u'START', u'TIME', u'COMMAND']}


So I used json.dumps and json.loads but the output was not a valid JSON.
I understand that JSON needs double quotes instead of simple quotes, but I don't think that the solution is to search and replace is the best way to resolve this problem.
Is there a more proper way to do it ?

Answer

Try this,

import json
data = {u'Processes': [[u'root', u'3606', u'0.0', u'0.2', u'76768', u'16664', u'?', u'Ss', u'20:40', u'0:01', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'4088', u'0.0', u'0.2', u'88544', u'20156', u'?', u'S', u'20:40', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'4090', u'0.0', u'0.2', u'88552', u'20140', u'?', u'S', u'20:40', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'4097', u'0.0', u'0.2', u'88552', u'20112', u'?', u'S', u'20:40', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'4110', u'0.0', u'0.2', u'88548', u'20160', u'?', u'S', u'20:40', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0']], u'Titles': [u'USER', u'PID', u'%CPU', u'%MEM', u'VSZ', u'RSS', u'TTY', u'STAT', u'START', u'TIME', u'COMMAND']}
data = json.dumps(data) # dict to string
data = json.loads(data) # string to json
print data['Processes']