Python Forum
json.dumps to keep dictionary keys
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
json.dumps to keep dictionary keys
#1
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
Reply
#2
result = {
        'RAM' : mem._asdict()
         }
 
print(json.dumps(result))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding keys and values to a dictionary giladal 3 597 Nov-19-2020, 04:58 PM
Last Post: deanhystad
  saving a dictionary as json file vinay_py 6 897 Jun-06-2020, 05:07 PM
Last Post: vinay_py
  access dictionary with keys from another and write values to list redminote4dd 6 1,127 Jun-03-2020, 05:20 PM
Last Post: DeaD_EyE
  Drop Keys From Dictionary donnertrud 8 1,195 May-30-2020, 11:39 AM
Last Post: DeaD_EyE
  json.dumps list output qurr 12 1,943 Apr-08-2020, 10:13 PM
Last Post: micseydel
  Not Getting the Desired value using json.dumps saurabh210 0 644 Feb-03-2020, 06:48 PM
Last Post: saurabh210
  json.dumps output error in python3 prayuktibid 2 954 Jan-21-2020, 06:41 AM
Last Post: prayuktibid
  Problem adding keys/values to dictionary where keynames = "property" and "value" jasonashaw 1 780 Dec-17-2019, 08:00 PM
Last Post: jasonashaw
  accessing Json dictionary lshankar 1 628 Dec-16-2019, 06:18 AM
Last Post: ndc85430
  Checking if the combination of two keys is in a dictionary? mrsenorchuck 6 1,452 Dec-04-2019, 10:35 AM
Last Post: mrsenorchuck

Forum Jump:

User Panel Messages

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