Create README.md
Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com>
This commit is contained in:
parent
0804da7344
commit
a0935be422
1 changed files with 106 additions and 0 deletions
106
python_basics/json/README.md
Normal file
106
python_basics/json/README.md
Normal file
|
@ -0,0 +1,106 @@
|
|||
# JSON and dict
|
||||
{:.no_toc}
|
||||
|
||||
<nav markdown="1" class="toc-class">
|
||||
* TOC
|
||||
{:toc}
|
||||
</nav>
|
||||
|
||||
## The goal
|
||||
|
||||
|
||||
|
||||
Questions to [David Rotermund](mailto:davrot@uni-bremen.de)
|
||||
|
||||
## [json -- JSON encoder and decoder](https://docs.python.org/3/library/json.html)
|
||||
|
||||
### [dump](https://docs.python.org/3/library/json.html#json.dump)
|
||||
|
||||
```python
|
||||
import json
|
||||
|
||||
a = dict(one=1, two=2, three=3)
|
||||
|
||||
with open("data_out.json", "w") as file:
|
||||
json.dump(a, file)
|
||||
```
|
||||
|
||||
Content of data_out.json:
|
||||
|
||||
```json
|
||||
{"one": 1, "two": 2, "three": 3}
|
||||
```
|
||||
|
||||
### [load](https://docs.python.org/3/library/json.html#json.load)
|
||||
|
||||
|
||||
```python
|
||||
import json
|
||||
|
||||
with open("data_out.json", "r") as file:
|
||||
b = json.load(file)
|
||||
|
||||
print(b)
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```python
|
||||
{'one': 1, 'two': 2, 'three': 3}
|
||||
```
|
||||
|
||||
## [jsmin](https://github.com/tikitu/jsmin/) and [loads](https://docs.python.org/3/library/json.html#json.loads)
|
||||
|
||||
```shell
|
||||
pip install jsmin
|
||||
```
|
||||
|
||||
Maybe you want to have comments in your config file.
|
||||
|
||||
Content of data_in.json:
|
||||
```json
|
||||
// This an important comment...
|
||||
{
|
||||
"one": 1,
|
||||
"two": 2,
|
||||
"three": 3
|
||||
}
|
||||
```
|
||||
|
||||
Note: You need to change the filetype from **JSON** to **JSON with comments**. Lower right corner.
|
||||
|
||||
The normal JSON functions fails:
|
||||
|
||||
```python
|
||||
import json
|
||||
|
||||
with open("data_in.json", "r") as file:
|
||||
b = json.load(file)
|
||||
|
||||
print(b)
|
||||
```
|
||||
Output:
|
||||
|
||||
```python
|
||||
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
|
||||
```
|
||||
|
||||
With jsmin:
|
||||
|
||||
```python
|
||||
import json
|
||||
from jsmin import jsmin
|
||||
|
||||
with open("data_in.json", "r") as file:
|
||||
minified = jsmin(file.read())
|
||||
|
||||
b = json.loads(minified)
|
||||
|
||||
print(b)
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```python
|
||||
{'one': 1, 'two': 2, 'three': 3}
|
||||
```
|
Loading…
Reference in a new issue