Skip to content
Related Articles

Related Articles

Improve Article
Communication Between two Programs using JSON
  • Last Updated : 27 Nov, 2020

JSON stands for JavaScript Object Notation. To communicate between computer and human we use a programming language. To communicate between two programs, there must be a common data format and here we use JSON format. Let us say you have an application with Frontend written in Python and Backend written in Java. Now you need to communicate between these two languages to do some stuff. (For example, a quiz website will take your input in Frontend and send data to Backend for calculating results and return the result to Frontend). 

So here we use JSON to communicate between two programs called Python and Java. We can use a common text format to communicate but text format will contain a lot of complexities. However, JSON is lightweight and easy to use. JSON is language-independent and hence can be used by any programming language.

Serialization:

Serialization is the process of converting programming data to JSON text. For example, in python, we can use dictionaries which are python objects to convert to JSON text. This process of converting python objects to JSON text is called Serialization.

Deserialization:



Deserialization is the reverse process of serialization. It is the process of converting JSON text to programming data. In Python, we can convert a JSON text to a Python object called a dictionary Python.

Transfer two variables from Python to Java:

To communicate between two programs we need to serialize data using a language and deserialize with other languages. Here we serialize data with Python and deserialize the same with Java. Thus, we transfer data from Python to Java. Here, let us transfer two variables ‘a’ and ‘b’ from Python to Java and calculate the result in Java.

Serialization using Python:

Let us generate a JSON file using python code as follows.

Python




# Import JSON 
import json
  
# using MAP
data = {"a" : 1, "b" : 2.3}
with open("JSONData.json", "w") as file:
    json.dump(data, file)
# JSONData.json file will be created

The JSONData.json file will contain the following content as follows.

Output:



{"a" : 1, "b" : 2.3}

Deserialization using Java:

In Java, there is no inbuilt library for JSON reader. We need to either add a dependency in our gradle project or download the jar file. We had Jackson and simple-json libraries. Here simple-json library is used.

Java




import java.io.*;
  
// Import JSON parser and JSONObject.
import org.json.simple.parser.*;
import org.json.simple.JSONObject;
  
// JSONFileRead is Main class
class JSONFileRead {
    public static void main(String[] args) throws FileNotFoundException, 
          IOException, ParseException {
            
        // File Handling Operations using JSON objects 
        // like JSON parser and JSONObject.
        FileReader fileReader = new FileReader("JSONData.json");
        JSONObject jsonObject = (JSONObject) new JSONParser().parse(fileReader);
         
        // Var a will take and get long value.
        // Var b will take and get double value.
        Long a = (Long)jsonObject.get("a");
        Double b = (Double)jsonObject.get("b");
          
        // Will print the result as sum of both the Variable.
        System.out.println("The sum of two variables is: " + (a + b));
    }
}

Output:

Here, you will see the result where a =1 and b= 2.3 as shown in the above program. Now, it will print the sum of both of them.

The sum of two variables is: 3.3

The example used is very small here. The same addition can be done in Python. However, in large projects, some complex computations can be done better in Java rather than Python. In that case, we use Java for calculations. The major applications of JSON data communication are APIs (Application Programming Interface).

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :