Python Forum
json.dumps to keep dictionary keys - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: json.dumps to keep dictionary keys (/thread-18356.html)



json.dumps to keep dictionary keys - batchenr - May-14-2019

Hello,
im using python3 on ubuntu 18 machine
i am new to python

i have this script :
import json
import psutil
d = {}

mem = psutil.virtual_memory()
dmem = mem._asdict()
print(json.dumps(dmem))
and i get this result :
Quote:["total": 4416118784, "available": 3831390208, "percent": 13.2]]
and so on..

i want to make an order here because im going to add
Quote:psutil.swap_memory()
psutil.cpu_times()
psutil.cpu_percent(interval=1, percpu=True)

so the qustions are :

if i want to make an order in the response like this :
result = {
        'RAM' : mem
         }

print(json.dumps(result))
i get the results without the keys :
Quote:["RAM": [4416118784, 3831066624, 13]

How can i keep the keys so it will look like this :
Quote:["RAM": [total : 4416118784, available : 3831066624, precent : 13]

thanks


RE: json.dumps to keep dictionary keys - buran - May-14-2019

result = {
        'RAM' : mem._asdict()
         }
 
print(json.dumps(result))