diff --git a/docs/scenarios/json.rst b/docs/scenarios/json.rst index 3683eecfc..29d87c44f 100644 --- a/docs/scenarios/json.rst +++ b/docs/scenarios/json.rst @@ -1,138 +1,52 @@ -JSON parsing -=========== +JSON +==== -json ------ +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. -`json `_ is a standard libary which can convert JSON to a Dictionay. +Parsing JSON +------------ -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: +The json libary is imported like this: .. 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 +Take the following string containing JSON data: .. code-block:: python - pip install simplejson - -`simplejson `_ is the externally maintained development version of the json library. + json_string = '{"first_name": "Guido", "last_name":"Rossum"}' -simplejson is updated much more frequently than the Python. Meaning you can get updates much quicker. - -For example, a JSON string like this: +It can be parsed like this: .. code-block:: python - "{'first_name':'Guido','last_name':'Rossum'}" + converted_dict = json.loads(json_string) -can be loaded like this: +and can now be used as a normal dictionary: .. code-block:: python - import simplejson - converted_dict = simplejson.loads(json_string) + print(converted_dict['first_name']) + "Guido" -you can now use it as a normal dictionary: +You can also convert a dictionary to JSON: .. 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: + print(json.dumps(d)) + "{'first_name':'Guido','last_name':'Rossum'}" -.. code-block:: python - import simplejson +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) +`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.