------------------------------------------------------------------
Python JSON(JavaScript Object Notation Serializer)
----------------------------------------------------------------
Purpose:
----------
-Encode Python objects as JSON strings, and decode JSON strings into Python objects.
-JSON is a syntax for storing and exchanging data.
-JSON is text, written with JavaScript object notation.
Uses:
------
-JSON (JavaScript Object Notation) is a lightweight data-interchange format
-The JSON format is often used for serializing and transmitting structured data over a network connection.
-It is used primarily to transmit data between a server and web application(client), serving as an alternative to XML.
-It is easy for humans to read and write.
-It is easy for machines to parse and generate.
-It is based on a subset of the JavaScript Programming Language, Standard ECMA
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You can convert Python objects of the following types, into JSON strings:
dict, list, tuple, string, int, float, True, False, None
Difference between XML and JSON:
--------------------------------------------
-Both JSON and XML can be used to receive data from a web server.
-JSON is a Data Oriented , XML is a Document Oriented
-JSON Support arrays, XML Not support array
-JSON less secured, XML more Secured
JSON Example:
------------------
{"employees":[
{ "firstName":"Jahab", "lastName":"Deen" },
{ "firstName":"Anna", "lastName":"Smith" },
{ "firstName":"Peter", "lastName":"Jones" }
]}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
XML Example:
------------------
<employees>
<employee>
<firstName>Jahab</firstName> <lastName>Deen</lastName>
</employee>
<employee>
<firstName>Anna</firstName> <lastName>Smith</lastName>
</employee>
<employee>
<firstName>Peter</firstName> <lastName>Jones</lastName>
</employee>
</employees>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Program 1: Convert from JSON to Python
import json
# some JSON:
x = '{ "name":"Jahab", "age":30, "city":"Trichy"}'
# parse x:
y = json.loads(x)
# the result is a Python dictionary:
print(y["name"])
print(y["age"])
print(y["city"])
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Program 2: Convert from Python to JSON
import json
# a Python object (dict):
x = {
"name": "Jahab",
"age": 30,
"city": "Trichy"
}
# convert into JSON:
y = json.dumps(x)
# the result is a JSON string:
print(y)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Note:
------
1) json.dumps(x, indent=4) - Format the result
2) json.dumps(x, indent=4, separators=(". ", " = ")) - Separators
3) json.dumps(x, indent=4, sort_keys=True) - Order the result
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Program 3: Convert from Python to JSON
import json
print(json.dumps({"name": "Jahab", "age": 30}))
print(json.dumps(["apple", "bananas"]))
print(json.dumps(("apple", "bananas")))
print(json.dumps("hello"))
print(json.dumps(42))
print(json.dumps(31.76))
print(json.dumps(True))
print(json.dumps(False))
print(json.dumps(None))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
No comments:
Post a Comment