JSON (JavaScript object notation)

Its syntax is derived from JavaScript object notation syntax. JSON is a syntax for storing and exchanging data.


Few important properties about JSON:


a) Data is in name/value pairs or key/value pairs

b) Data is separated by commas

c) Curly braces hold objects

d) Square brackets hold arrays


Valid Data Types in JSON, values must be one of the following data types:

a) a string

b) a number

c) an object (JSON object)

d) an array

e) a boolean

f) null


Note :
JSON cannot be an object. JSON is a string format.


Difference between XML * JSON

XML has to be parsed with an XML parser. JSON can be parsed by a standard JavaScript function.


Python has a built-in package called json

import json


#1 Convert from JSON to Python:
import json

x = '{"name" : "Shakti", "age" : 30, "city" : "Bangalore"}'
y = json.loads(x)
print(y["name"])



#Convert from Python to JSON :
import json

#2 a Python object (dict):
x = {
  "name": "John",
  "age": 30,
  "city": "New York"
}
# convert into JSON:
y = json.dumps(x)

# the result is a JSON string:
print(y)