27 JSON module in python

 JSON is a syntax for storing and exchanging data.


JSON is text, written with JavaScript object notation.


import json

Example

Convert from JSON to Python:


import json


# some JSON:

x =  '{ "name":"John", "age":30, "city":"New York"}'


# parse x:

y = json.loads(x)


# the result is a Python dictionary:

print(y["age"])


Example

Convert from Python to JSON:


import json


# 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)

Comments

Popular posts from this blog

1 PYTHON PROGRAMMING

16 file handling in python

4 Tuple data types