Reputation: 191
I have this script, and I have a custom json example. I need to get the complete json and save it in a file in the same position.
dic = {'window': set_window, 'window.screen': window_screen, 'window.navigator' : window_navigator }
json=json.dumps(dic, indent=5, sort_keys=False)
print json
The order at this moment is correct, and I need to print it in the same order. I have been trying for 2 days and it still fails :(( Is it possible to retain the order of the JSON object as I want it?
Upvotes: 0
Views: 822
Reputation: 4653
I need to get the complete json and save it in a file in the same position.
You shouldn't rely on position to update a file containing JSON.
First of all, it would be wise to separate the configuration you seem to store in JSON from the actual code.
So let's say you have a file containing JSON:
config.json
{
"window": {
"innerHeight": 886,
"innerWidth": 1280
},
"window.screen": {
"availWidth": 1280,
"availTop": 0,
"availHeight": 1024,
"availLeft": 0
},
"window.navigator": {
"oscpu": "Windows NT 6.3; WOW64",
"product": "Gecko",
"vendor": "",
"buildID": "20140923175406",
"appName": "Netscape",
"appCodeName": "Mozilla",
"productSub": "20100101",
"plugins": {
"1": {
"application/sdp": {
"type": "application/sdp",
"description": "Playing SDP-files",
"suffixes": "sdp"
},
"name": "QuickTime Plug-in 7.7.5",
"filename": "npqtplugin.dll",
"0": {
"type": "application/sdp",
"description": "Playing SDP-files",
"suffixes": "sdp"
},
"length": 1,
"version": "7.7.7.5",
"description": "The QuickTime Plugin allows you to view a wide variety of multimedia content in Web pages. For more information, visit the QuickTime Web site."
},
"0": {
"1": {
"type": "application/futuresplash",
"description": "FutureSplash movie",
"suffixes": "spl"
},
"0": {
"type": "application/x-shockwave-flash",
"description": "Adobe Flash movie",
"suffixes": "swf"
},
"length": 2,
"version": "12.0.0.43",
"name": "Shockwave Flash",
"application/futuresplash": {
"type": "application/futuresplash",
"description": "FutureSplash movie",
"suffixes": "spl"
},
"application/x-shockwave-flash": {
"type": "application/x-shockwave-flash",
"description": "Adobe Flash movie",
"suffixes": "swf"
},
"filename": "NPSWF32_12_0_0_43.dll",
"description": "Shockwave Flash 12.0 r.43"
},
"length": 2,
"Shockwave Flash": {
"application/futuresplash": {
"description": "FutureSplash movie",
"suffixes": "spl"
},
"description": "Shockwave Flash 12.0 r.43",
"application/x-shockwave-flash": {
"description": "Adobe Flash movie",
"suffixes": "swf"
},
"filename": "NPSWF32_12_0_0_43.dll",
"version": "12.0.0.43",
"name": "Shockwave Flash"
}
},
"userAgent": "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0.3",
"language": "en",
"appVersion": "5.0 (Windows)",
"platform": "x86",
"vendorSub": ""
}
}
What you'll need to do to update the JSON stored in the file is to read the file, use json.loads
to load the JSON into a python dict, update anything you might want and save the output of json.dumps
with the updated keys and values to the same file. So you'll end up having something like this somewhere in your code:
import json
with open('prueba.json', 'r') as f:
config = json.loads(f.read())
with open('prueba.json', 'w') as f:
# Here I update window settings as an example
config['window']['innerHeight'] = 1440
config['window']['innerWidth'] = 900
f.write(json.dumps(config))
Upvotes: 1
Reputation: 36023
Here's the basic idea for how it could be done:
class CustomOrderStr(str):
def __lt__(self, other):
# Replace with whatever comparison logic you want. This just reverses order.
return not str.__lt__(self, other)
dic = {'a': 1, 'b': 2, 'c': 3}
print json.dumps(dic, indent=5, sort_keys=True)
print json.dumps({CustomOrderStr(key): value for key, value in dic.iteritems()}, indent=5, sort_keys=True)
Output:
{
"a": 1,
"b": 2,
"c": 3
}
{
"c": 3,
"b": 2,
"a": 1
}
However I have to agree with others that if you want a custom order for JSON you're probably doing something bad.
Upvotes: 0