JSON(JavaScript ऑब्जेक्ट नोटेशन) हे एक लोकप्रिय डेटा फॉरमॅट आहे जे ऍप्लिकेशन्समधील डेटा एक्सचेंजसाठी वापरले जाते. Python मॉड्यूलद्वारे JSON हाताळणीचे समर्थन करते json, तुम्हाला डेटा आणि JSON फॉरमॅटमध्ये रूपांतरित करण्याची परवानगी देते Python.
JSON सह काम करण्यासाठी येथे पायऱ्या आहेत Python:
Python डेटा JSON मध्ये रूपांतरित करा
वापरा json.dumps(): Python ऑब्जेक्ट(सूची, शब्दकोश, टपल, इ.) JSON स्ट्रिंगमध्ये रूपांतरित करा.
वापरा json.dump(): Python JSON फाइलवर डेटा लिहा.
JSON Python डेटामध्ये रूपांतरित करा
वापरा json.loads(): JSON स्ट्रिंगला ऑब्जेक्टमध्ये रूपांतरित करा Python(सूची, शब्दकोश, टपल इ.).
वापरा json.load(): JSON फाईलमधील डेटा वाचा आणि डेटामध्ये रूपांतरित करा Python.
उदाहरण:
import json
# Convert Python data to JSON
data_dict = {"name": "John", "age": 30, "city": "New York"}
json_string = json.dumps(data_dict)
print(json_string) # Output: {"name": "John", "age": 30, "city": "New York"}
# Write Python data to a JSON file
with open("data.json", "w") as f:
json.dump(data_dict, f)
# Convert JSON to Python data
json_data = '{"name": "John", "age": 30, "city": "New York"}'
python_dict = json.loads(json_data)
print(python_dict) # Output: {'name': 'John', 'age': 30, 'city': 'New York'}
# Read data from a JSON file and convert to Python data
with open("data.json", "r") as f:
data_dict = json.load(f)
print(data_dict) # Output: {'name': 'John', 'age': 30, 'city': 'New York'}
लक्षात घ्या की JSON वापरताना,, , Python सारखे विशेष डेटा प्रकार त्यांच्या संबंधित JSON प्रतिनिधित्वांमध्ये रूपांतरित केले जातील: ,, अनुक्रमे. None True False null true false

