Benefits of JSON in CherryPy🤩
JSON delivers similar kind of benefits that XML accomplishes for transferring data in a heterogeneous setting, as the following:
⏩ JSON is self-narrating. The syntax and structure of the JSON strings can be decrypted by applications that arent aware of what data to expect.
⏩ JSON is easy text. This makes it convenient and secure for sharing across platforms and OS that do not share more complicated document types. As text, JSON can also be readily exhibited and edited in easy editors.
⏩ JSON is a compact prototype. An intermediate JSON string is nearly two-thirds of the size of the data in XML.
⏩ JSON can be quickly learned as it is uncomplicated to read, and easy to understand.
Dealing with JSON in CherryPy🤝
CherryPy has support integrated for JSON encoding responses and decoding of requests.
Decoding request using JSON in CherryPy📭🔓
To spontaneous decode the contents of a request using JSON:
Add a processor to parse the JSON request entity: The default processor puts the parsed data into request.json. Provide your own processor to use a custom decoder or process the parsed data differently. Processors can be configured via the tools.json_in.processor or decorator methods.
class Root(object):
@cherrypy.expose
@cherrypy.tools.json_in()
def index(self):
data = cherrypy.request.json

You can also try this code with Online Javascript Compiler
Run Code
The JSON attribute linked to the request includes the decoded content.
Encoding response using JSON in CherryPy📪🔒
To encode the contents of a response using JSON:
Add a processor to parse JSON request entities: The default processor places the parsed data into request.json. The processor can be configured via cherrypy.lib.jsontools.json_out(content_type='application/json', debug=False, handler=<function json_handler>)
class root(object):
@cherrypy.expose
@cherrypy.tools.json_out()
def index(self):
return {'key': 'value'}

You can also try this code with Online Javascript Compiler
Run Code
CherryPy will encode any content produced by the page handler utilizing JSON. Not all the types of objects may naturally be encoded.
Receiving JSON in a POST request in CherryPy
Here is Code in Python to import cherrypy followed by code in Javascript to receive JSON
import cherrypy
class Root:
@cherrypy.expose
@cherrypy.tools.json_out()
@cherrypy.tools.json_in()
def my_route(self):
result = {"operation": "request", "result": "success"}
input_json = cherrypy.request.json
value = input_json["my_key"]
# Responses are serialized to JSON (because of the json_out decorator)
return result

You can also try this code with Online Python Compiler
Run Code
//assuming you're using jQuery
var myObject = { "my_key": "my_value" };
$.ajax({
type: "POST",
url: "my_route",
data: JSON.stringify(myObject),
contentType: 'application/json',
dataType: 'json',
error: function() {
alert("error");
},
success: function() {
alert("success");
}
});

You can also try this code with Online Javascript Compiler
Run Code
Resources📚
Until now, we’ve learned the basics of JSON along with a few of its operations. Still, feeling unfamiliar with JSON in CherryPy? Don't worry! We’ve got your back covered with articles that’ll give you just the right information to get started with.😉
JSON introduction and Syntax🤓
Commands of Json👩💻
Basic of Json with Golang🙂
PHP and JSON💻
Java developers-Working with JSON😎
GWT JSON😊
Frequently asked questions🤔❓
Is JSON a programming language?
No. JSON is a data format that could be classified as a language but not as a programming language. Its association with JavaScript is that it shares its syntax with a subset of JavaScript literals.
Which programming languages are supported by JSON?
JSON is a text form entirely language-independent but operates using routines that are standard to programmers of C, C++, C#, Java, JavaScript, Perl, Python, and others.
Can we use comments inside a JSON file?
No. The JSON file should all be data. If comments are included, then they will be considered as data too.
What is JSON Syntax?
JSON syntax emanated from JavaScript object notation syntax. Data is in the form of name/value pairs and is separated by commas. Curly braces hold objects. Square brackets contain arrays.
What are the limitations of JSON?
JSON is not the appropriate fit for addressing complex data. When the data gets complicated with multiple nested structures, it becomes difficult for human readability. JSON does not allow comments. It also does not support handling multimedia formats.
Conclusion 🏁
This article provides a brief overview of dealing with JSON in CherryPy. Now that you're familiar with CherryPy, learn how to configure application settings, Architecture of Config In CherryPy, Namespaces in CherryPy, and perform arithmetic in CherryPy python, or Start with the basics of Python.
Happy learning!
Thank you