| Start | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Storing data in a file The following snippet encodes the data stored in |
||||||||||||||||||||||||||||||||
| Retrieving data from a file The following snippet opens a JSON encoded file (replace |
||||||||||||||||||||||||||||||||
| Formatting JSON output Let's say we have the following data: Just dumping this as JSON does not do anything special here: Setting indentation to get prettier outputIf we want pretty printing, we can set an Sorting keys alphabetically to get consistent outputBy default the order of keys in the output is undefined. We can get them in alphabetical order to make sure we always get the same output: Getting rid of whitespace to get compact outputWe might want to get rid of the unnecessary spaces, which is done by setting separator strings different from the default |
||||||||||||||||||||||||||||||||
Creating JSON from Python dict The above snippet will return the following: |
||||||||||||||||||||||||||||||||
Creating Python dict from JSON The above snippet will return the following: |
||||||||||||||||||||||||||||||||
load vs loads, dump vs dumps The Here we use the string-based functions: And here we use the file-based functions: As you can see the main difference is that when dumping json data you must pass the file handle to the function, as opposed to capturing the return value. Also worth noting is that you must seek to the start of the file before reading or writing, in order to avoid data corruption. When opening a file the cursor is placed at position Having both ways of dealing with json data allows you to idiomatically and efficiently work with formats which build upon json, such as |
||||||||||||||||||||||||||||||||
Calling json.tool from the command line to pretty-print JSON output Given some JSON file "foo.json" like: we can call the module directly from the command line (passing the filename as an argument) to pretty-print it: The module will also take input from STDOUT, so (in Bash) we equally could do: |
||||||||||||||||||||||||||||||||
| JSON encoding custom objects If we just try the following: we get an error saying To be able to serialize the datetime object properly, we need to write custom code for how to convert it: and then use this encoder class instead of RemarksFor full documentation including version-specific functionality, please check the official documentation (opens new window). TypesDefaultsthe De-serialisation types:
The Serialisation types:
To disallow encoding of Custom (de-)serialisationThere are various hooks which allow you to handle data that needs to be represented differently. Use of Serialisation:You can provide a function that operates on objects before they are serialised like so: De-serialisation:There are various hooks that are handled by the json functions, such as object_hook and parse_float. For an exhaustive list for your version of python, see here (opens new window). Further custom (de-)serialisation:The |
Read Article
https://develop.horje.com/learn/1434/reference