Table of contents
1.
Introduction🤓
2.
About JSON😊 
3.
Benefits of JSON in CherryPy🤩
4.
Dealing with JSON in CherryPy🤝 
4.1.
Decoding request using JSON in CherryPy📭🔓
4.2.
Encoding response using JSON in CherryPy📪🔒
4.3.
Receiving JSON in a POST request in CherryPy
5.
Resources📚
6.
Frequently asked questions🤔❓
6.1.
Is JSON a programming language?
6.2.
Which programming languages are supported by JSON?
6.3.
Can we use comments inside a JSON file?
6.4.
What is JSON Syntax?
6.5.
What are the limitations of JSON?
7.
Conclusion 🏁
Last Updated: Mar 27, 2024
Medium

Dealing with JSON in cherrypy

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction🤓

Hey Ninjas! Another day of learning and exploring? You’ve come to just the right place.

Today lets learn how to deal with JSON in CherryPy along with a few operations such as decoding requests and encoding responses using JSON in CherryPy. 

Dealing with JSON in cherrypy

So what are you waiting for? Let's get rolling with JSON in CherryPy with the introduction and benefits of JSON. 

About JSON😊 

JSON stands for JavaScript Object Notation.😃 It is a lightweight text-based open prototype developed for human-readable data dealings. It is plain text authored in JavaScript object notation. It is utilized to send data across computers. The JSON filename extension is .json. The JSON format is syntactically identical to the code for constructing JavaScript objects. Because of this, the JavaScript program can effortlessly convert JSON data into JavaScript objects. Due to the format being text only, JSON data can smoothly be transferred between systems and utilized by any programming language.

The properties that make JSON an ideal data-interchange language are stated below.

JSON is constructed on two structures:

⏩A group of name/value pairs. In different languages, this is recognized as an object, dictionary, record, struct, keyed list, hash table, or associative array.

⏩The presence of an ordered list of values. In most languages, this is recognized as an array, vector, sequence, or list.
 

These are versatile data structures. Nearly all modern programming languages support them in one or the other form. It is a data format that is convertible with programming languages that are based on these structures.

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 settingsArchitecture of Config In CherryPyNamespaces in CherryPy, and perform arithmetic in CherryPy python, or Start with the basics of Python.

Happy learning!

Thank you

Live masterclass