1

my data

(('check_kvserver_mem_4500000', 2737L), 
 ('check_ethstatus', 250L), 
 ('check_ddos', 229L), 
 ('check_coredump', 193L),
 ('check_robot', 174L),
 ('check_disk_90_95', 155L))

into this:print json.dumps(data)

[["check_kvserver_mem_4500000", 2737], 
 ["check_ethstatus", 250],
 ["check_ddos", 229],
 ["check_coredump", 193], 
 ["check_disk_90_95", 155]]

I want data like

{["check_kvserver_mem_4500000", 2737],
 ["check_ethstatus", 250]
 ["check_ddos", 229], 
 ["check_coredump", 193], 
 ["check_disk_90_95", 155]}

or

 {"check_kvserver_mem_4500000":2737,
  "check_ethstatus":250,
  "check_ddos":229, 
  "check_coredump":193,
  "check_disk_90_95":155}
Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
haha
  • 11
  • 1
  • 3

3 Answers3

4

You want a JSON Object as a result but your data has no key/value structure. So json.dumps(data) is doing the right thing in generating a JSON List, not an Object. A Python tuple of tuples can not be matched on an invalid JSON Object.

Edit

After you changed your question (and deleted my beautiful edits while doing this), your question can be answered.

data = (('check_kvserver_mem_4500000', 2737L),
        ('check_ethstatus', 250L),
        ('check_ddos', 229L),
        ('check_coredump', 193L),
        ('check_robot', 174L),
        ('check_disk_90_95', 155L))
print json.dumps(dict(data))

Result is:

'{"check_disk_90_95": 155, "check_coredump": 193, "check_robot": 174,
  "check_kvserver_mem_4500000": 2737, "check_ddos": 229, "check_ethstatus": 250}'
2
 print json.dumps(dict(data))
 {"check_disk_90_95": 155,
  "check_coredump": 193,
  "check_robot": 174,
  "check_kvserver_mem_4500000": 2737,
  "check_ddos": 229,
  "check_ethstatus": 250}
Rakesh
  • 81,458
  • 17
  • 76
  • 113
1
d = dict((('check_kvserver_mem_4500000', 2737L), ('check_ethstatus', 250L), ('check_ddos', 229L), ('check_coredump', 193L), ('check_robot',
174L), ('check_disk_90_95', 155L)))

import json 

print json.dumps(x, indent = 5)

Returns

{
     "check_disk_90_95": 155, 
     "check_coredump": 193, 
     "check_robot": 174, 
     "check_kvserver_mem_4500000": 2737, 
     "check_ddos": 229, 
     "check_ethstatus": 250
}
Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169