1
- JSON parsing
1
+ JSON
2
2
===========
3
3
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.
6
5
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.
8
9
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:
10
13
11
14
.. code-block :: python
12
15
13
- " {'first_name':'Guido','last_name':'Rossum'} "
16
+ import json
14
17
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:
16
25
17
26
.. code-block :: python
18
27
19
- import json
20
28
converted_dict = json.loads(json_string)
21
29
22
- you can now use it as a normal dictionary:
30
+ and can now be used as a normal dictionary:
23
31
24
32
.. code-block :: python
25
33
26
34
converted_dict[' first_name' ]
27
35
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:
31
37
32
38
.. code-block :: python
33
39
@@ -36,25 +42,24 @@ For example, given:
36
42
' second_name' : ' Rossum'
37
43
}
38
44
39
- import json
40
- print json.dumps(d)
45
+ print (json.dumps(d))
41
46
" {'first_name':'Guido','last_name':'Rossum'}"
42
47
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 `` :
44
49
45
50
.. code-block :: python
46
51
47
- import json
48
52
with file (' path/to/file.json' ) as json_file:
49
53
processed_json = json.load(json_file)
50
- print processsed_json
54
+
55
+ print (processsed_json)
51
56
{u ' first_name' : u ' Guido' , u ' last_name' : u ' Rossum' }
52
57
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 ``:
54
60
55
61
.. code-block :: python
56
62
57
- import json
58
63
with file (' path/to/file.json' , ' w' ) as json_file:
59
64
dict = {
60
65
" first_name" : " Guido" ,
@@ -65,74 +70,22 @@ As well as write to them:
65
70
66
71
simplejson
67
72
----------
68
-
69
- Installation
70
-
71
- .. code-block :: python
72
-
73
- pip install simplejson
74
-
75
73
`simplejson <https://simplejson.readthedocs.org/en/latest/ >`_ is the externally maintained development version of the json library.
76
74
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
99
76
100
- For example, given:
77
+ Installation
101
78
102
79
.. code-block :: python
103
80
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
113
82
114
- It is also possible to import JSON files:
83
+ Usage
115
84
116
85
.. code-block :: python
117
86
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
128
88
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.
130
90
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
91
0 commit comments