-
Notifications
You must be signed in to change notification settings - Fork 5.9k
add initial JSON section #507
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| JSON parsing | ||
| =========== | ||
|
|
||
| json | ||
| ----- | ||
|
|
||
| `json <https://docs.python.org/2/library/json.html>`_ 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'}" | ||
|
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. This doesn't actually work. It needs to be: '{"first_name":"Guido","last_name":"Rossum"}'Note the types of quotes used. You should also assign this to
Author
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. I think it would be best to copy and paste the string. Will try both ways and see what looks the nicest |
||
|
|
||
| can be loaded like this: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| import json | ||
| converted_dict = json.loads(json_string) | ||
|
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. This should be called |
||
|
|
||
| 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 | ||
|
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. "As well as converting a string containing JSON". A "JSON string" would be converted to a Python string |
||
|
|
||
| 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'}" | ||
|
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. Again, you have your usage of quotes backwards. |
||
|
|
||
| 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 | ||
|
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. Use parentheses please. This will ensure people using Python 3 can test this code as well even if they're beginners and have only learned Python 3 (and therefore don't know that |
||
| {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 <https://simplejson.readthedocs.org/en/latest/>`_ 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) | ||
|
|
||
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.
Should note that it was added in 2.6. There's
simplejsonon PyPI for all older versions (since there are disappointingly still a lot of people using 2.4 and 2.5)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.
Ahh ok maybe simple JSON doesn't need to be a separate section then? Could roll it all into one section. With a foot note explaining what simple JSON is
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.
Looks like your later statement that it's still maintained/released is true. I wasn't aware of that. Leave the extra section for now. I have research to do ;)