JavaScript Object Notation is a built-in python package. Using JSON can work with JSON data JSON is used for exchanging and storing data. It is text. We can also use the JSON package in Odoo. Simply import the JSON package then we can able to use JSON
import json
JSON Objects include parsing, serializing, deserializing, and many more. Those are the JSON package providing essential tools.
To encode native data type to JSON format is called serialization process. For example, consider a python dictionary using python JSON can convert to a JSON object. Similarly, int and float converted like JSON numbers, lists, and tuples can be converted into JSON array; JSON null results from None converted.
How do we serialize Python data to JSON format? Let’s take a look at these methods:
a) Dump()
b) Dumps()
We can use dumps() by json.dumps() function that converts a Python object into a json string.
Syntax:
json.dumps(obj)
For example, an object is a dictionary it can write as json.dumps(Dictionary)
return json.dumps({'id': order_id.id})
json.dump() used to write to json file
json.dump(dictionary, storable_file_name)
In json.dump, the output has to be stored as an argument, so it needs a JSON file name
Deserializing methods
a) json.loads()
b) json.load()
Exactly deserializing is just the opposite of serializing. If you have already encoded an object, then use a deserializing method to decode that object again.
Syntax
json.loads()
json.loads(s) ,the arguments can be string,array,byte
json.loads() are also used to convert python dictionary
json.loads(result) it return also a python object
Let's see an example,
task= {
'parent_id': task_obj.id,
'name': rec.product_id.name
}
json.loads(task)
Consider we need to return a JSON object, so that time, we can use a json.load()
Syntax:
json.load()
json.load(object_of _file)
url=https://api.github.com
res = json.load(urlopen(f'https://api.github.com))
Encoding Complex Numbers
Encoding is the process of converting the values or text into an encrypted form.
Commonly, it is impossible to pass a complex number into JSON. You get an error like TypeError: Object of type 'complex' is not JSON serializable if you try to encode.
In this case, we can create a function that checks whether it is complex or not. But in python, you can find the real and imaginary parts of the object. real and object.imag
complex_ob= json.dumps(2+ 3j, default=complexnumber)
print(complex_object)
import json
def comple_xnumber(object):
if isinstance(object, complex):
return [object.real, object.imag]
raise TypeError(repr(object) + " is not JSON serialized")
You can see that the function complex_number checks whether it’s complex or not and then splits it into the real and imaginary parts.
The result is
[2.0,3 .0]
Here consider that 2.0 is the real part and 3.0 is the imaginary part.
Complex JSON object decoding in Python
Using the object hook parameter, we can decode complex objects in JSON. Object hook checks JSON string contains the complex object or not.
So using python.json we can exchange and sort data more easily. The effective method of sorting and exchanging data is serializing and deserializing. Commonly impossible to pass a complex number into JSON, but it can be easily handled with the function to check whether it is complex or not.