|
| 1 | +JSON parsing |
| 2 | +=========== |
| 3 | + |
| 4 | +json |
| 5 | +----- |
| 6 | + |
| 7 | +`json <https://docs.python.org/2/library/json.html>`_ is a standard libary which can convert JSON to a Dictionay. |
| 8 | + |
| 9 | +For example, a JSON string like this: |
| 10 | + |
| 11 | +.. code-block:: python |
| 12 | +
|
| 13 | + "{'first_name':'Guido','last_name':'Rossum'}" |
| 14 | +
|
| 15 | +can be loaded like this: |
| 16 | + |
| 17 | +.. code-block:: python |
| 18 | +
|
| 19 | + import json |
| 20 | + converted_dict = json.loads(json_string) |
| 21 | +
|
| 22 | +you can now use it as a normal dictionary: |
| 23 | + |
| 24 | +.. code-block:: python |
| 25 | +
|
| 26 | + converted_dict['first_name'] |
| 27 | +
|
| 28 | +As well as converting a JSON string to a dictionary. You can convert a dictionary to JSON |
| 29 | + |
| 30 | +For example, given: |
| 31 | + |
| 32 | +.. code-block:: python |
| 33 | +
|
| 34 | + d = { |
| 35 | + 'first_name': 'Guido', |
| 36 | + 'second_name': 'Rossum' |
| 37 | + } |
| 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 |
| 56 | +
|
| 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 | +
|
| 66 | +simplejson |
| 67 | +---------- |
| 68 | + |
| 69 | +Installation |
| 70 | + |
| 71 | +.. code-block:: python |
| 72 | +
|
| 73 | + pip install simplejson |
| 74 | +
|
| 75 | +`simplejson <https://simplejson.readthedocs.org/en/latest/>`_ is the externally maintained development version of the json library. |
| 76 | + |
| 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 | +
|
0 commit comments