-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Improvements to the JSON section #511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sigmavirus24
merged 3 commits into
realpython:master
from
samjacobclift:improve_json_section
Jan 18, 2015
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,138 +1,52 @@ | ||
| JSON parsing | ||
| =========== | ||
| JSON | ||
| ==== | ||
|
|
||
| json | ||
| ----- | ||
| The `json <https://docs.python.org/2/library/json.html>`_ 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 <https://docs.python.org/2/library/json.html>`_ 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need a separate block to demonstrate how to import the library. |
||
| 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 <https://simplejson.readthedocs.org/en/latest/>`_ 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 <https://simplejson.readthedocs.org/en/latest/>`_ 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. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a new line after this.