json — JavaScript Object Notation

Purpose: Encode Python objects as JSON strings, and decode JSON strings into Python objects.

The json module provides an API similar to pickle for converting in-memory Python objects to a serialized representation known as JavaScript Object Notation (JSON). Unlike pickle, JSON has the benefit of having implementations in many languages (especially JavaScript). It is most widely used for communicating between the web server and client in a REST API, but is also useful for other inter-application communication needs.

The json library can parse JSON from strings or files. The library parses JSON into a Python dictionary or list. It can also convert Python dictionaries or lists into JSON strings.


In [1]:
json_string = '{"first_name": "Guido", "last_name":"Rossum"}'

In [2]:
import json
parsed_json = json.loads(json_string)

In [3]:
print(parsed_json['first_name'])


Guido

In [4]:
d = {
    'first_name': 'Guido',
    'second_name': 'Rossum',
    'titles': ['BDFL', 'Developer'],
}

print(json.dumps(d))


{"first_name": "Guido", "second_name": "Rossum", "titles": ["BDFL", "Developer"]}
Python JSON
dict object
list, tuple array
str string
int, float, int- & float-derived Enums number
True true
False false
None null

In [ ]:
Encode Python objects as JSON strings

In [ ]:
json.dump(obj, fp,
          skipkeys=False,
          ensure_ascii=True,
          check_circular=True,
          allow_nan=True,
          cls=None,
          indent=None,
          separators=None,
          default=None,
          sort_keys=False, **kw)

In [ ]:
#Examples : Python Dictionaries to JSON strings

import json
student = {"101":{"class":'V', "Name":'Rohit',  "Roll_no":7},
           "102":{"class":'V', "Name":'David',  "Roll_no":8},
           "103":{"class":'V', "Name":'Samiya', "Roll_no":12}}
print(json.dumps(student));

In [ ]:
#Examples : Python Dictionaries to JSON strings (sorted by key)


import json
student = {"101":{"class":'V', "Name":'Rohit',  "Roll_no":7},
           "102":{"class":'V', "Name":'David',  "Roll_no":8},
           "103":{"class":'V', "Name":'Samiya', "Roll_no":12}}
print(json.dumps(student, sort_keys=True));

In [ ]:
#Examples : Python tuple to JSON array


import json
tup1 = 'Red', 'Black', 'White';
print(json.dumps(tup1));

In [ ]:
#Examples : Python list to JSON array


import json
list1 = [5, 12, 13, 14];
print(json.dumps(list1));

In [ ]:
#Examples : Python string to JSON string


import json
string1 = 'Python and JSON';
print(json.dumps(string1));

In [ ]:
#Examples : Python Boolean values to JSON Boolean values

import json
x = True;
print(json.dumps(x));

In [ ]:
#Examples : Python int, float, int- & float-derived Enums to JSON number


import json
x = -456;
y = -1.406;
z =  2.12e-10
print(json.dumps(x));
print(json.dumps(y));
print(json.dumps(z));

In [ ]:
Decode JSON strings into Python objects

In [ ]:
json.load(fp, 
          cls=None, 
		  object_hook=None, 
		  parse_float=None, 
		  parse_int=None, 
		  parse_constant=None, 
		  object_pairs_hook=None, **kw)

In [ ]:

JSON Python
object dict
array list
string str
number (int) int
number (real) float
true True
false False
null None

In [ ]:
#Examples : JSON strings to Python Dictionaries


import json
json_data = '{"103": {"class": "V", "Name": "Samiya", "Roll_n": 12}, "102": {"class": "V", "Name": "David", "Roll_no": 8}, "101": {"class": "V", "Name": "Rohit", "Roll_no": 7}}';
print(json.loads(json_data));

In [ ]:
# Examples : JSON array Python tuple

import jsonJson_array = ["Red", "Black", "White"]
print(json.dumps(Json_array));

In [ ]:
#Examples : Python list to JSON array


import json 
Json_string = "Python and JSON" 
print(json.dumps(Json_string));

In [ ]:


In [ ]:


In [ ]: