Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/contents.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ different scenarios.
scenarios/scientific
scenarios/imaging
scenarios/xml
scenarios/json
scenarios/crypto


Expand Down
138 changes: 138 additions & 0 deletions docs/scenarios/json.rst
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.

Copy link
Copy Markdown
Contributor

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 simplejson on PyPI for all older versions (since there are disappointingly still a lot of people using 2.4 and 2.5)

Copy link
Copy Markdown
Author

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

Copy link
Copy Markdown
Contributor

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 ;)


For example, a JSON string like this:

.. code-block:: python

"{'first_name':'Guido','last_name':'Rossum'}"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 json_string so it's usage in the next example makes sense. Alternatively, don't use the variable name and just copy and paste the string into the call to json.loads

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be called loaded_json. You should also explain how { ... } in JSON is a representation of an Object in Javascript and that it's equivalent simple representation in Python is as a dictionary.


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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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'}"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 print was ever a statement)

{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)