Skip to content

Commit f5ee4e2

Browse files
author
Sam Clift
committed
Improvements to the JSON section
Thanks to https://github.com/mplewis
1 parent 4db76aa commit f5ee4e2

File tree

1 file changed

+30
-77
lines changed

1 file changed

+30
-77
lines changed

docs/scenarios/json.rst

Lines changed: 30 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,39 @@
1-
JSON parsing
1+
JSON
22
===========
33

4-
json
5-
-----
4+
The `json <https://docs.python.org/2/library/json.html>`_ library can read JSON strings into a Python dictionary or array. It can also serialize Python dictionaries or arrays into JSON strings.
65

7-
`json <https://docs.python.org/2/library/json.html>`_ is a standard libary which can convert JSON to a Dictionay.
6+
* There are six basic types in JSON: objects, arrays, numbers, strings, booleans, and null.
7+
* 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.
8+
* JSON does not use single quotes. JSON exclusively uses double quotes. Using single quotes in the place of double quotes is invalid JSON syntax.
89

9-
For example, a JSON string like this:
10+
Parsing JSON
11+
------------
12+
The `json <https://docs.python.org/2/library/json.html>`_ libary is imported like this:
1013

1114
.. code-block:: python
1215
13-
"{'first_name':'Guido','last_name':'Rossum'}"
16+
import json
1417
15-
can be loaded like this:
18+
Take the following string containing JSON data:
19+
20+
.. code-block:: python
21+
22+
json_string = '{"first_name": "Guido", "last_name":"Rossum"}'
23+
24+
It can be manpulated like this:
1625

1726
.. code-block:: python
1827
19-
import json
2028
converted_dict = json.loads(json_string)
2129
22-
you can now use it as a normal dictionary:
30+
and can now be used as a normal dictionary:
2331

2432
.. code-block:: python
2533
2634
converted_dict['first_name']
2735
28-
As well as converting a JSON string to a dictionary. You can convert a dictionary to JSON
29-
30-
For example, given:
36+
As well as converting a JSON string to a dictionary. You can convert a dictionary to JSON:
3137

3238
.. code-block:: python
3339
@@ -36,25 +42,24 @@ For example, given:
3642
'second_name': 'Rossum'
3743
}
3844
39-
import json
40-
print json.dumps(d)
45+
print(json.dumps(d))
4146
"{'first_name':'Guido','last_name':'Rossum'}"
4247
43-
It is also possible to import JSON files:
48+
We can also load a JSON file by using ``json.load`` instead of ``json.loads``:
4449

4550
.. code-block:: python
4651
47-
import json
4852
with file('path/to/file.json') as json_file:
4953
processed_json = json.load(json_file)
50-
print processsed_json
54+
55+
print(processsed_json)
5156
{u'first_name': u'Guido', u'last_name': u'Rossum'}
5257
53-
As well as write to them:
58+
59+
Here's an example of writing directly to a file by using ``json.dump`` instead of ``json.dumps``:
5460

5561
.. code-block:: python
5662
57-
import json
5863
with file('path/to/file.json', 'w') as json_file:
5964
dict = {
6065
"first_name": "Guido",
@@ -65,74 +70,22 @@ As well as write to them:
6570
6671
simplejson
6772
----------
68-
69-
Installation
70-
71-
.. code-block:: python
72-
73-
pip install simplejson
74-
7573
`simplejson <https://simplejson.readthedocs.org/en/latest/>`_ is the externally maintained development version of the json library.
7674

77-
simplejson is updated much more frequently than the Python. Meaning you can get updates much quicker.
78-
79-
For example, a JSON string like this:
80-
81-
.. code-block:: python
82-
83-
"{'first_name':'Guido','last_name':'Rossum'}"
84-
85-
can be loaded like this:
86-
87-
.. code-block:: python
88-
89-
import simplejson
90-
converted_dict = simplejson.loads(json_string)
91-
92-
you can now use it as a normal dictionary:
93-
94-
.. code-block:: python
95-
96-
converted_dict['first_name']
97-
98-
As well as converting a json string to dictionarys. You can convert dictionarys to json
75+
simplejson mimics the json standard library, so you can start using simplejson instead of json by importing it under a different name
9976

100-
For example, given:
77+
Installation
10178

10279
.. code-block:: python
10380
104-
import simplejson
105-
106-
d = {
107-
'first_name': 'Guido',
108-
'second_name': 'Rossum'
109-
}
110-
print simplejson.dumps(d)
111-
"{'first_name':'Guido','last_name':'Rossum'}"
112-
81+
pip install simplejson
11382
114-
It is also possible to import JSON files:
83+
Usage
11584

11685
.. code-block:: python
11786
118-
import simplejson
119-
120-
with file('path/to/file.json') as json_file:
121-
processed_json = simplejson.load(json_file)
122-
print processsed_json
123-
{u'first_name': u'Guido', u'last_name': u'Rossum'}
124-
125-
As well as write to them:
126-
127-
.. code-block:: python
87+
import simplejson as json
12888
129-
import simplejson
89+
simplejson is available so that developers that use an older version of python can use the latest features available in the json lib.
13090

131-
with file('path/to/file.json', 'w') as json_file:
132-
dict = {
133-
"first_name": "Guido",
134-
"last_name": "Rossum",
135-
"middle_name": "Van"
136-
}
137-
simplejson.dump(dict, json_file)
13891

0 commit comments

Comments
 (0)