From 2e2d70ca9be42ae644d202fdf5c7673dae65c566 Mon Sep 17 00:00:00 2001 From: Matthew Lewis Date: Thu, 5 Feb 2015 14:03:18 -0600 Subject: [PATCH] Add info on working with file objects --- docs/scenarios/json.rst | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/docs/scenarios/json.rst b/docs/scenarios/json.rst index 2ca49e8..98a0292 100644 --- a/docs/scenarios/json.rst +++ b/docs/scenarios/json.rst @@ -39,6 +39,36 @@ You can also convert the following to JSON: print(json.dumps(d)) '{"first_name": "Guido", "last_name": "Rossum", "titles": ["BDFL", "Developer"]}' +Working with File Objects +------------------------- + +We can also load a JSON file by using ``json.load`` instead of ``json.loads``: + +.. code-block:: python + + import json + + with file('path/to/file.json') as f: + loaded_json = json.load(f) + + print(loaded_json) # {'first_name': 'Ada', 'last_name': 'Lovelace'} + +Here's an example of writing directly to a file by using ``json.dump`` instead of ``json.dumps``: + +.. code-block:: python + + import json + + my_data = { + 'name': 'Alan Turing', + 'played_by': 'Benedict Cumberbatch' + } + + with file('path/to/file.json', 'w') as f: + json.dump(my_data, f) + +``path/to/file.json`` now contains a JSON representation of the my_data dictionary. + simplejson ----------