|
1 | | -JSON parsing |
2 | | -=========== |
| 1 | +JSON |
| 2 | +==== |
3 | 3 |
|
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. |
6 | 5 |
|
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 | +------------ |
8 | 8 |
|
9 | | -For example, a JSON string like this: |
| 9 | +Take the following string containing JSON data: |
10 | 10 |
|
11 | 11 | .. code-block:: python |
12 | 12 |
|
13 | | - "{'first_name':'Guido','last_name':'Rossum'}" |
| 13 | + json_string = '{"first_name": "Guido", "last_name":"Rossum"}' |
14 | 14 |
|
15 | | -can be loaded like this: |
| 15 | +It can be parsed like this: |
16 | 16 |
|
17 | 17 | .. code-block:: python |
18 | 18 |
|
19 | 19 | import json |
20 | | - converted_dict = json.loads(json_string) |
| 20 | + parsed_json = json.loads(json_string) |
21 | 21 |
|
22 | | -you can now use it as a normal dictionary: |
| 22 | +and can now be used as a normal dictionary: |
23 | 23 |
|
24 | 24 | .. code-block:: python |
25 | 25 |
|
26 | | - converted_dict['first_name'] |
| 26 | + print(parsed_json['first_name']) |
| 27 | + "Guido" |
27 | 28 |
|
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: |
31 | 30 |
|
32 | 31 | .. code-block:: python |
33 | 32 |
|
34 | 33 | d = { |
35 | 34 | 'first_name': 'Guido', |
36 | | - 'second_name': 'Rossum' |
| 35 | + 'second_name': 'Rossum', |
| 36 | + 'titles': ['BDFL', 'Developer'], |
37 | 37 | } |
38 | 38 |
|
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"]}' |
56 | 41 |
|
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) |
65 | 42 |
|
66 | 43 | simplejson |
67 | 44 | ---------- |
68 | 45 |
|
69 | | -Installation |
70 | | - |
71 | | -.. code-block:: python |
72 | | -
|
73 | | - pip install simplejson |
74 | | -
|
75 | 46 | `simplejson <https://simplejson.readthedocs.org/en/latest/>`_ is the externally maintained development version of the json library. |
76 | 47 |
|
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