📜
JSON | Veri Bilimi
Web üzerindeki JavaScript verileri (json) yapısı, okuma ve temel işlemler

❔ JSON Nedir

    Internet üzeriindenki çoğu veri JSON tipindedir
      Web üzeride Javascript çok fazla kullanılmaktadır
      Google, Twitter vs.
    JavaScript Object Notation olarak açılır
    JavaScript'te bilgileri aktarmak için kullanılır

🏗️ JSON Yapısı

    dict ve list'in harmanlanmış hali denebilir
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
4
book1 = {
5
'title': 'The Prophet',
6
'author': 'Khalil Gibran',
7
'genre': 'poetry',
8
'tags': [
9
'religion',
10
'spirituality',
11
'philosophy',
12
'Lebanon',
13
'Arabic',
14
'Middle East',
15
],
16
'book_id': '811.19',
17
'copies': [{'edition_year': 1996, 'checkouts': 486,
18
'borrowed': False}, {'edition_year': 1996,
19
'checkouts': 443, 'borrowed': False}],
20
}
21
book2 = {
22
'title': 'The Little Prince',
23
'author': 'Antoine de Saint-Exupery',
24
'genre': 'children',
25
'tags': ['fantasy', 'France', 'philosophy', 'illustrated', 'fable'
26
],
27
'id': '843.912',
28
'copies': [{
29
'edition_year': 1983,
30
'checkouts': 634,
31
'borrowed': True,
32
'due_date': '2017/02/02',
33
}, {'edition_year': 2015, 'checkouts': 41, 'borrowed': False}],
34
}
35
library = [book1, book2]
36
library
37
Copied!

⭐ JSON Örneği

1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
[{
4
'title': 'The Prophet',
5
'author': 'Khalil Gibran',
6
'genre': 'poetry',
7
'tags': [
8
'religion',
9
'spirituality',
10
'philosophy',
11
'Lebanon',
12
'Arabic',
13
'Middle East',
14
],
15
'book_id': '811.19',
16
'copies': [{'edition_year': 1996, 'checkouts': 486,
17
'borrowed': False}, {'edition_year': 1996,
18
'checkouts': 443, 'borrowed': False}],
19
}, {
20
'title': 'The Little Prince',
21
'author': 'Antoine de Saint-Exupery',
22
'genre': 'children',
23
'tags': ['fantasy', 'France', 'philosophy', 'illustrated', 'fable'
24
],
25
'id': '843.912',
26
'copies': [{
27
'edition_year': 1983,
28
'checkouts': 634,
29
'borrowed': True,
30
'due_date': '2017/02/02',
31
}, {'edition_year': 2015, 'checkouts': 41, 'borrowed': False}],
32
}]
33
Copied!

👀 JSON Okuma

    JSON dosyaları f.read() şeklinde değil, json.load(f) şekinde okunur
f, dosya objesi

🏈 f.read

1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
with open('./data/library.json', 'r') as f:
4
library_string = f.read()
5
Copied!
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
'''[\n {\n "title": "The Prophet",\n "author": "Khalil Gibran",\n "genre": "poetry",\n "tags": [\n "religion",\n "spirituality",\n "philosophy",\n "Lebanon",\n "Arabic",\n "Middle East"\n ],\n "book_id": "811.19",\n "copies": [\n {\n "edition_year": 1996,\n "checkouts": 486,\n "borrowed": false\n },\n {\n "edition_year": 1996,\n "checkouts": 443,\n "borrowed": false\n }\n ]\n },\n {\n "title": "The Little Prince",\n "author": "Antoine de Saint-Exupery",\n "genre": "children",\n "tags": [\n "fantasy",\n "France",\n "philosophy",\n "illustrated",\n "fable"\n ],\n "id": "843.912",\n "copies": [\n {\n "edition_year": 1983,\n "checkouts": 634,\n "borrowed": true,\n "due_date": "2017/02/02"\n },\n {\n "edition_year": 2015,\n "checkouts": 41,\n "borrowed": false\n }\n ]\n }\n]'''
4
Copied!

🏀 json.load(f)

1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
with open('./data/library.json', 'r') as f:
4
reloaded_library = json.load(f)
5
reloaded_library
6
Copied!
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
[{
4
'title': 'The Prophet',
5
'author': 'Khalil Gibran',
6
'genre': 'poetry',
7
'tags': [
8
'religion',
9
'spirituality',
10
'philosophy',
11
'Lebanon',
12
'Arabic',
13
'Middle East',
14
],
15
'book_id': '811.19',
16
'copies': [{'edition_year': 1996, 'checkouts': 486,
17
'borrowed': False}, {'edition_year': 1996,
18
'checkouts': 443, 'borrowed': False}],
19
}, {
20
'title': 'The Little Prince',
21
'author': 'Antoine de Saint-Exupery',
22
'genre': 'children',
23
'tags': ['fantasy', 'France', 'philosophy', 'illustrated', 'fable'
24
],
25
'id': '843.912',
26
'copies': [{
27
'edition_year': 1983,
28
'checkouts': 634,
29
'borrowed': True,
30
'due_date': '2017/02/02',
31
}, {'edition_year': 2015, 'checkouts': 41, 'borrowed': False}],
32
}]
33
34
Copied!

🌍 URL ile JSON okuma

1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
pd.read_json('https://api.github.com/repos/pydata/pandas/issues?per_page=5')
Copied!
Last modified 1yr ago