Skip to content

Commit c4d63d6

Browse files
committed
Merge branch 'pr/511'
2 parents cb1ddc6 + f1e196a commit c4d63d6

File tree

1 file changed

+18
-108
lines changed

1 file changed

+18
-108
lines changed

docs/scenarios/json.rst

Lines changed: 18 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,48 @@
1-
JSON parsing
2-
===========
1+
JSON
2+
====
33

4-
json
5-
-----
4+
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.
65

7-
`json <https://docs.python.org/2/library/json.html>`_ is a standard libary which can convert JSON to a Dictionay.
6+
Parsing JSON
7+
------------
88

9-
For example, a JSON string like this:
9+
Take the following string containing JSON data:
1010

1111
.. code-block:: python
1212
13-
"{'first_name':'Guido','last_name':'Rossum'}"
13+
json_string = '{"first_name": "Guido", "last_name":"Rossum"}'
1414
15-
can be loaded like this:
15+
It can be parsed like this:
1616

1717
.. code-block:: python
1818
1919
import json
20-
converted_dict = json.loads(json_string)
20+
parsed_json = json.loads(json_string)
2121
22-
you can now use it as a normal dictionary:
22+
and can now be used as a normal dictionary:
2323

2424
.. code-block:: python
2525
26-
converted_dict['first_name']
26+
print(parsed_json['first_name'])
27+
"Guido"
2728
28-
As well as converting a JSON string to a dictionary. You can convert a dictionary to JSON
29-
30-
For example, given:
29+
You can also convert a the following to JSON:
3130

3231
.. code-block:: python
3332
3433
d = {
3534
'first_name': 'Guido',
36-
'second_name': 'Rossum'
35+
'second_name': 'Rossum',
36+
'titles': ['BDFL', 'Developer'],
3737
}
3838
39-
import json
40-
print json.dumps(d)
41-
"{'first_name':'Guido','last_name':'Rossum'}"
42-
43-
It is also possible to import JSON files:
44-
45-
.. code-block:: python
46-
47-
import json
48-
with file('path/to/file.json') as json_file:
49-
processed_json = json.load(json_file)
50-
print processsed_json
51-
{u'first_name': u'Guido', u'last_name': u'Rossum'}
52-
53-
As well as write to them:
54-
55-
.. code-block:: python
39+
print(json.dumps(d))
40+
'{"first_name": "Guido", "last_name": "Rossum", "titles": ["BDFL", "Developer"]}'
5641
57-
import json
58-
with file('path/to/file.json', 'w') as json_file:
59-
dict = {
60-
"first_name": "Guido",
61-
"last_name": "Rossum",
62-
"middle_name": "Van"
63-
}
64-
json.dump(dict, json_file)
6542
6643
simplejson
6744
----------
6845

69-
Installation
70-
71-
.. code-block:: python
72-
73-
pip install simplejson
74-
7546
`simplejson <https://simplejson.readthedocs.org/en/latest/>`_ is the externally maintained development version of the json library.
7647

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
99-
100-
For example, given:
101-
102-
.. code-block:: python
103-
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-
113-
114-
It is also possible to import JSON files:
115-
116-
.. code-block:: python
117-
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
128-
129-
import simplejson
130-
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)
138-
48+
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.

0 commit comments

Comments
 (0)