From f5ee4e207a693ff409f83cb608215eb97979fe1e Mon Sep 17 00:00:00 2001 From: Sam Clift Date: Wed, 14 Jan 2015 15:09:50 +0000 Subject: [PATCH 1/3] Improvements to the JSON section Thanks to https://github.com/mplewis --- docs/scenarios/json.rst | 107 +++++++++++----------------------------- 1 file changed, 30 insertions(+), 77 deletions(-) diff --git a/docs/scenarios/json.rst b/docs/scenarios/json.rst index 3683eecfc..8a6e4928f 100644 --- a/docs/scenarios/json.rst +++ b/docs/scenarios/json.rst @@ -1,33 +1,39 @@ -JSON parsing +JSON =========== -json ------ +The `json `_ library can read JSON strings into a Python dictionary or array. It can also serialize Python dictionaries or arrays into JSON strings. -`json `_ is a standard libary which can convert JSON to a Dictionay. +* There are six basic types in JSON: objects, arrays, numbers, strings, booleans, and null. +* The root element of JSON representation is an object, signified by ``{ ... }``. JSON objects are analogous to Python dictionaries: they have keys which correspond to values. +* JSON does not use single quotes. JSON exclusively uses double quotes. Using single quotes in the place of double quotes is invalid JSON syntax. -For example, a JSON string like this: +Parsing JSON +------------ +The `json `_ libary is imported like this: .. code-block:: python - "{'first_name':'Guido','last_name':'Rossum'}" + import json -can be loaded like this: +Take the following string containing JSON data: + +.. code-block:: python + + json_string = '{"first_name": "Guido", "last_name":"Rossum"}' + +It can be manpulated like this: .. code-block:: python - import json converted_dict = json.loads(json_string) -you can now use it as a normal dictionary: +and can now be used 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: +As well as converting a JSON string to a dictionary. You can convert a dictionary to JSON: .. code-block:: python @@ -36,25 +42,24 @@ For example, given: 'second_name': 'Rossum' } - import json - print json.dumps(d) + print(json.dumps(d)) "{'first_name':'Guido','last_name':'Rossum'}" -It is also possible to import JSON files: +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 json_file: processed_json = json.load(json_file) - print processsed_json + + print(processsed_json) {u'first_name': u'Guido', u'last_name': u'Rossum'} -As well as write to them: + +Here's an example of writing directly to a file by using ``json.dump`` instead of ``json.dumps``: .. code-block:: python - import json with file('path/to/file.json', 'w') as json_file: dict = { "first_name": "Guido", @@ -65,74 +70,22 @@ As well as write to them: 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 +simplejson mimics the json standard library, so you can start using simplejson instead of json by importing it under a different name -For example, given: +Installation .. code-block:: python - import simplejson - - d = { - 'first_name': 'Guido', - 'second_name': 'Rossum' - } - print simplejson.dumps(d) - "{'first_name':'Guido','last_name':'Rossum'}" - + pip install simplejson -It is also possible to import JSON files: +Usage .. 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 as json - import simplejson +simplejson is available so that developers that use an older version of python can use the latest features available in the json lib. - 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) From 332fdcd6f184bceb7037c1fa0933fe982dc1eae4 Mon Sep 17 00:00:00 2001 From: Sam Clift Date: Wed, 14 Jan 2015 15:19:24 +0000 Subject: [PATCH 2/3] arrays to lists --- docs/scenarios/json.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/scenarios/json.rst b/docs/scenarios/json.rst index 8a6e4928f..c5857310c 100644 --- a/docs/scenarios/json.rst +++ b/docs/scenarios/json.rst @@ -1,9 +1,9 @@ JSON =========== -The `json `_ library can read JSON strings into a Python dictionary or array. It can also serialize Python dictionaries or arrays into JSON strings. +The `json `_ library can read JSON strings into a Python dictionary or list. It can also serialize Python dictionaries or lists into JSON strings. -* There are six basic types in JSON: objects, arrays, numbers, strings, booleans, and null. +* There are six basic types in JSON: objects, lists, numbers, strings, booleans, and null. * The root element of JSON representation is an object, signified by ``{ ... }``. JSON objects are analogous to Python dictionaries: they have keys which correspond to values. * JSON does not use single quotes. JSON exclusively uses double quotes. Using single quotes in the place of double quotes is invalid JSON syntax. From 37ed5fc40a75b60d2d3cb9f4e8f11ce6a7f32d96 Mon Sep 17 00:00:00 2001 From: Sam Clift Date: Sun, 18 Jan 2015 14:22:30 +0000 Subject: [PATCH 3/3] update based on PR feedback --- docs/scenarios/json.rst | 59 +++++++---------------------------------- 1 file changed, 10 insertions(+), 49 deletions(-) diff --git a/docs/scenarios/json.rst b/docs/scenarios/json.rst index c5857310c..29d87c44f 100644 --- a/docs/scenarios/json.rst +++ b/docs/scenarios/json.rst @@ -1,15 +1,12 @@ JSON -=========== +==== -The `json `_ library can read JSON strings into a Python dictionary or list. It can also serialize Python dictionaries or lists into JSON strings. - -* There are six basic types in JSON: objects, lists, numbers, strings, booleans, and null. -* The root element of JSON representation is an object, signified by ``{ ... }``. JSON objects are analogous to Python dictionaries: they have keys which correspond to values. -* JSON does not use single quotes. JSON exclusively uses double quotes. Using single quotes in the place of double quotes is invalid JSON syntax. +The `json `_ library can parse JSON from strings or files. When parsing, the library converts the JSON into a Python dictionary or list. It can also parse Python dictionaries or lists into JSON strings. Parsing JSON ------------ -The `json `_ libary is imported like this: + +The json libary is imported like this: .. code-block:: python @@ -21,7 +18,7 @@ Take the following string containing JSON data: json_string = '{"first_name": "Guido", "last_name":"Rossum"}' -It can be manpulated like this: +It can be parsed like this: .. code-block:: python @@ -31,9 +28,10 @@ and can now be used as a normal dictionary: .. code-block:: python - converted_dict['first_name'] + print(converted_dict['first_name']) + "Guido" -As well as converting a JSON string to a dictionary. You can convert a dictionary to JSON: +You can also convert a dictionary to JSON: .. code-block:: python @@ -45,47 +43,10 @@ As well as converting a JSON string to a dictionary. You can convert a dictionar print(json.dumps(d)) "{'first_name':'Guido','last_name':'Rossum'}" -We can also load a JSON file by using ``json.load`` instead of ``json.loads``: - -.. code-block:: python - - 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'} - - -Here's an example of writing directly to a file by using ``json.dump`` instead of ``json.dumps``: - -.. code-block:: python - - 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 ---------- -`simplejson `_ is the externally maintained development version of the json library. - -simplejson mimics the json standard library, so you can start using simplejson instead of json by importing it under a different name - -Installation - -.. code-block:: python - - pip install simplejson - -Usage - -.. code-block:: python - - import simplejson as json - -simplejson is available so that developers that use an older version of python can use the latest features available in the json lib. +`simplejson `_ is the externally maintained development version of the json library. +simplejson mimics the json standard library, it is available so that developers that use an older version of python can use the latest features available in the json lib.