From 49988bf59ca78ae96bea2cb000c5f19b3b43cdcd Mon Sep 17 00:00:00 2001 From: Sam Clift Date: Thu, 11 Dec 2014 15:31:31 +0000 Subject: [PATCH] add initial JSON section --- docs/contents.rst.inc | 1 + docs/scenarios/json.rst | 138 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 docs/scenarios/json.rst diff --git a/docs/contents.rst.inc b/docs/contents.rst.inc index 8e8cd73..cc38532 100644 --- a/docs/contents.rst.inc +++ b/docs/contents.rst.inc @@ -59,6 +59,7 @@ different scenarios. scenarios/scientific scenarios/imaging scenarios/xml + scenarios/json scenarios/crypto diff --git a/docs/scenarios/json.rst b/docs/scenarios/json.rst new file mode 100644 index 0000000..3683eec --- /dev/null +++ b/docs/scenarios/json.rst @@ -0,0 +1,138 @@ +JSON parsing +=========== + +json +----- + +`json `_ is a standard libary which can convert JSON to a Dictionay. + +For example, a JSON string like this: + +.. code-block:: python + + "{'first_name':'Guido','last_name':'Rossum'}" + +can be loaded like this: + +.. code-block:: python + + import json + converted_dict = json.loads(json_string) + +you can now use it as a normal dictionary: + +.. code-block:: python + + converted_dict['first_name'] + +As well as converting a JSON string to a dictionary. You can convert a dictionary to JSON + +For example, given: + +.. code-block:: python + + d = { + 'first_name': 'Guido', + 'second_name': 'Rossum' + } + + import json + print json.dumps(d) + "{'first_name':'Guido','last_name':'Rossum'}" + +It is also possible to import JSON files: + +.. code-block:: python + + import json + with file('path/to/file.json') as json_file: + processed_json = json.load(json_file) + print processsed_json + {u'first_name': u'Guido', u'last_name': u'Rossum'} + +As well as write to them: + +.. code-block:: python + + import json + with file('path/to/file.json', 'w') as json_file: + dict = { + "first_name": "Guido", + "last_name": "Rossum", + "middle_name": "Van" + } + json.dump(dict, json_file) + +simplejson +---------- + +Installation + +.. code-block:: python + + pip install simplejson + +`simplejson `_ is the externally maintained development version of the json library. + +simplejson is updated much more frequently than the Python. Meaning you can get updates much quicker. + +For example, a JSON string like this: + +.. code-block:: python + + "{'first_name':'Guido','last_name':'Rossum'}" + +can be loaded like this: + +.. code-block:: python + + import simplejson + converted_dict = simplejson.loads(json_string) + +you can now use it as a normal dictionary: + +.. code-block:: python + + converted_dict['first_name'] + +As well as converting a json string to dictionarys. You can convert dictionarys to json + +For example, given: + +.. code-block:: python + + import simplejson + + d = { + 'first_name': 'Guido', + 'second_name': 'Rossum' + } + print simplejson.dumps(d) + "{'first_name':'Guido','last_name':'Rossum'}" + + +It is also possible to import JSON files: + +.. code-block:: python + + import simplejson + + with file('path/to/file.json') as json_file: + processed_json = simplejson.load(json_file) + print processsed_json + {u'first_name': u'Guido', u'last_name': u'Rossum'} + +As well as write to them: + +.. code-block:: python + + import simplejson + + with file('path/to/file.json', 'w') as json_file: + dict = { + "first_name": "Guido", + "last_name": "Rossum", + "middle_name": "Van" + } + simplejson.dump(dict, json_file) +