json format in python

Encode Python objects as JSON strings, and decode JSON strings into Python objects

In Python, the json module provides an API similar to convert in-memory Python objects to a serialized representation known as JavaScript Object Notation (JSON) and vice-a-versa.

Encode Python objects as JSON strings

Basic Usage :


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)

The above method serialize obj as a JSON formatted stream to fp (a .write()-supporting file-like object) using the following conversion table.

Python JSON
dict object
list, tuple array
str string
int, float, int- & float-derived Enums number
True true
False false
None null

Options :

  • The default value of skipkeys is False. If skipkeys is True, then dict keys that are not of a basic type (str, int, float, bool, None) will be skipped instead of raising a TypeError.
  • The json module always produces str objects, not bytes objects. Therefore, fp.write() must support str input.
  • If ensure_ascii is True (the default), the output is guaranteed to have all incoming non-ASCII characters escaped. If ensure_ascii is False, these characters will be output as-is.
  • The default value of check_circular is True. If check_circular is False, then the circular reference check for container types will be skipped and a circular reference will result in an OverflowError.
  • The default value of allow_nan is True. If allow_nan is False, then it will be a ValueError to serialize out of range float values (nan, inf, -inf) in strict compliance with the JSON specification, instead of using the JavaScript equivalents (NaN, Infinity, -Infinity).
  • If indent is a non-negative integer or string, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0, negative, or "" will only insert newlines. None (the default) selects the most compact representation. Using a positive integer indent indents that many spaces per level. If indent is a string (such as "\t"), that string is used to indent each level.
  • Use (',', ': ') as default if indent is not None.
  • The default value of sort_keys is False. If sort_keys is True, then the output of dictionaries will be sorted by key.

Examples : Python Dictionaries to JSON strings

Code :


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

Output:

{"103": {"class": "V", "Name": "Samiya", "Roll_no": 12}, 
"102": {"class": "V", "Name": "David", "Roll_no": 8}, 
"101": {"class": "V", "Name": "Rohit", "Roll_no": 7}}

Examples : Python Dictionaries to JSON strings (sorted by key)

Code :


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

Output:

{"101": {"Name": "Rohit", "Roll_no": 7, "class": "V"}, 
"102": {"Name": "David", "Roll_no": 8, "class": "V"}, 
"103": {"Name": "Samiya", "Roll_no": 12, "class": "V"}}

Examples : Python tuple to JSON array

Code :


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

Output:

["Red", "Black", "White"]

Examples : Python list to JSON array

Code :


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

Output :

[5, 12, 13, 14]

Examples : Python string to JSON string

Code :


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

Output:

 

##################JSON AND REST ####################

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}}
#Encode
print(json.dumps(student));

#Encode
s=json.dumps(student)

# Decode
print(json.loads(s));

tup1 = ('Red', 'Black', 'White');
print(json.dumps(tup1));
#Encode
s=json.dumps(tup1)
#Decode
print(json.loads(s));

list1 = [5, 12, 13, 14];
print(json.dumps(list1));
#Encode
s=json.dumps(list1)
#Decode
print(json.loads(s));

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

#Encode
s=json.dumps(string1)
#Decode
print(json.loads(s));

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

s=json.dumps(x)
#Decode
print(json.loads(s));

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

 

 

######################################

GET and POST  in web client and web server

#########################

 

GET and POST requests using Python

HTTP (Hypertext Transfer Protocol) request methods  GET and POST requests in Python.

What is HTTP?
HTTP is a set of protocols designed to enable communication between clients and servers. It works as a request-response protocol between a client and server.

A web browser may be the client, and an application on a computer that hosts a web site may be the server.

So, to request a response from the server, there are mainly two methods:

  1. GET : to request data from the server.
  2. POST : to submit data to be processed to the server.

Here is a simple diagram which explains the basic concept of GET and POST methods.iservice_post_get
Now, to make HTTP requests in python, we can use several HTTP libraries like:

The most elegant and simplest of above listed libraries is Requests. We will be using requests library in this article. To download and install Requests library, use following command:

pip install requests

Making a Get request

# importing the requests library
import requests
# api-endpoint
# location given here
location = "delhi technological university"
# defining a params dict for the parameters to be sent to the API
PARAMS = {'address':location}
# sending get request and saving the response as response object
r = requests.get(url = URL, params = PARAMS)
# extracting data in json format
data = r.json()
# extracting latitude, longitude and formatted address 
# of the first matching location
latitude = data['results'][0]['geometry']['location']['lat']
longitude = data['results'][0]['geometry']['location']['lng']
formatted_address = data['results'][0]['formatted_address']
# printing the output
print("Latitude:%s\nLongitude:%s\nFormatted Address:%s"
%(latitude, longitude,formatted_address))

 

Output:
1

The above example finds latitude, longitude and formatted address of a given location by sending a GET request to the Google Maps API. An API (Application Programming Interface) enables you to access the internal features of a program in a limited fashion. And in most cases, the data provided is in JSON(JavaScript Object Notation) format (which is implemented as dictionary objects in Python!).

Important points to infer :

  • PARAMS = {'address':location}

    The URL for a GET request generally carries some parameters with it. For requests library, parameters can be defined as a dictionary. These parameters are later parsed down and added to the base url or the api-endpoint.
    To understand the parameters role, try to print r.url after the response object is created. You will see something like this:

    http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university

    This is the actual URL on which GET request is made

  • r = requests.get(url = URL, params = PARAMS)

    Here we create a response object ‘r’ which will store the request-response. We use requests.get() method since we are sending a GET request. The two arguments we pass are url and the parameters dictionary.

  • data = r.json()

    Now, in order to retrieve the data from the response object, we need to convert the raw response content into a JSON type data structure. This is achieved by using json() method. Finally, we extract the required information by parsing down the JSON type object.

Making a POST request

# importing the requests library
import requests
# defining the api-endpoint 
# your API key here
API_KEY = "XXXXXXXXXXXXXXXXX"
# your source code here
source_code = '''
print("Hello, world!")
a = 1
b = 2
print(a + b)
'''
# data to be sent to api
data = {'api_dev_key':API_KEY,
'api_option':'paste',
'api_paste_code':source_code,
'api_paste_format':'python'}
# sending post request and saving response as response object
r = requests.post(url = API_ENDPOINT, data = data)
# extracting response text 
pastebin_url = r.text
print("The pastebin URL is:%s"%pastebin_url)

This example explains how to paste your source_code to pastebin.com by sending POST request to the PASTEBIN API.
First of all, you will need to generate an API key by signing up here and then access your API key here.

Important features of this code:

  • data = {'api_dev_key':API_KEY,
            'api_option':'paste',
            'api_paste_code':source_code,
            'api_paste_format':'python'}

    Here again, we will need to pass some data to API server. We store this data as a dictionary.

  • r = requests.post(url = API_ENDPOINT, data = data)

    Here we create a response object ‘r’ which will store the request-response. We use requests.post() method since we are sending a POST request. The two arguments we pass are url and the data dictionary.

  • pastebin_url = r.text

    In response, the server processes the data sent to it and sends the pastebin URL of your source_code which can be simply accessed by r.text .

requests.post method could be used for many other tasks as well like filling and submitting the web forms, posting on your FB timeline using the Facebook Graph API, etc.

Here are some important points to ponder upon:

  • When the method is GET, all form data is encoded into the URL, appended to the action URL as query string parameters. With POST, form data appears within the message body of the HTTP request.
  • In GET method, the parameter data is limited to what we can stuff into the request line (URL). Safest to use less than 2K of parameters, some servers handle up to 64K.No such problem in POST method since we send data in message body of the HTTP request, not the URL.
  • Only ASCII characters are allowed for data to be sent in GET method.There is no such restriction in POST method.
  • GET is less secure compared to POST because data sent is part of the URL. So, GET method should not be used when sending passwords or other sensitive information.

 

REST FRAMEOWRK

 

https://medium.com/backticks-tildes/lets-build-an-api-with-django-rest-framework-32fcf40231e5

 

 

 

####################### JSON using local DJSNGO setup#########################

 

>> import requests
>>> URL = "http://127.0.0.1:8000/student/"
>>> r = requests.get(url = URL, params = PARAMS)
>>> data=r.json()
>>> print(data)
[{'id': 1, 'name': 'a', 'mail': '[email protected]', 'course': 'python', 'phonenumber': 87866788}, {'id': 2, 'name': 'b', 'mail': '[email protected]', 'course': 'python', 'phonenumber': 44567767288}, {'id': 3, 'name': 'c', 'mail': '[email protected]', 'course': 'Java', 'phonenumber': 9887667879}]
>>> print(data[0]['mail'])
[email protected]
>>>

 

#######################################