json_string = '{"first_name": "Guido", "last_name":"Rossum"}'

import json
parsed_json = json.loads(json_string)

print(parsed_json['first_name'])

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

print(json.dumps(d))

"""
Explanation: 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.
End of explanation
"""


Encode Python objects as JSON strings

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)

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


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


#Examples : Python tuple to JSON array


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


#Examples : Python list to JSON array


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


#Examples : Python string to JSON string


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


#Examples : Python Boolean values to JSON Boolean values

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


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


Decode JSON strings into Python objects



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

"""
Explanation: |Python |JSON|
|:---|:---|
|dict   |object|
|list, tuple    |array|
|str    |string|
|int, float, int- & float-derived Enums |number|
|True   |true|
|False  |false|
|None   |null|
End of explanation
"""


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


# Examples : JSON array Python tuple

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

#Examples : Python list to JSON array


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


"""
Explanation: |JSON|Python|
|:---|:---|
|object|dict|
|array|list|
|string|str|
|number (int)|int|
|number (real)|float|
|true|True|
|false|False|
|null|None|
End of explanation
"""


