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
  I'm assuming you're asking about accessing nested dictionary keys and values in Pytho DavidSah 0 580 Dec-18-2025, 01:54 AM
Last Post: DavidSah
Question [SOLVED] Access keys and values from nested dictionary? Winfried 4 914 Nov-17-2025, 11:47 AM
Last Post: Winfried
Question How do I skipkeys on json file read to python dictionary? BrandonKastning 3 3,394 Mar-08-2022, 09:34 PM
Last Post: BrandonKastning
  Adding keys and values to a dictionary giladal 3 4,736 Nov-19-2020, 04:58 PM
Last Post: deanhystad
  saving a dictionary as json file vinay_py 6 5,448 Jun-06-2020, 05:07 PM
Last Post: vinay_py
  access dictionary with keys from another and write values to list redminote4dd 6 5,958 Jun-03-2020, 05:20 PM
Last Post: DeaD_EyE
  Drop Keys From Dictionary donnertrud 8 6,528 May-30-2020, 11:39 AM
Last Post: DeaD_EyE
  json.dumps list output qurr 12 9,206 Apr-08-2020, 10:13 PM
Last Post: micseydel
  Not Getting the Desired value using json.dumps saurabh210 0 2,384 Feb-03-2020, 06:48 PM
Last Post: saurabh210
  json.dumps output error in python3 prayuktibid 2 4,430 Jan-21-2020, 06:41 AM
Last Post: prayuktibid

Forum Jump:

User Panel Messages

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