Python JSON

Python dasturlash tilida JSON elementlari va uning ustida ishlash uchun modul mavjud buning uchun quyidagicha chaqirish kifoya.

import json

JSON (text) ni Pythonda o'qish

Agar sizda JSON satri bo'lsa, uni json.loads() funksiyasi yordamida tahlil qilishingiz mumkin.

import json

x =  '{ "name":"Farrux", "age":26, "city":"Tashkent"}'
y = json.loads(x)
print(y["age"])
26

Python JSON ni JSON qatoriga ga o'tkazish

Agar sizda Python ob'ekti bo'lsa, uni json.dumps()funksiyasi yordamida JSON qatoriga aylantirishingiz mumkin.

import json

x = {
  "name""Farrux",
  "age"26,
  "city""Tashkent"
}

y = json.dumps(x)

print(y)
{ "name":"Farrux", "age":26, "city":"Tashkent"}

Siz quyidagi turdagi Python obyektlarini JSON satrlariga aylantirishingiz mumkin:

  • dict
  • list
  • tuple
  • string
  • int
  • float
  • True
  • False
  • None