How to communicate JSON data between Python and Node.js?


JSON  can be abbreviated as JavaScript Object Notation. It is a text-based file used for transferring and storing data in programming languages. It is supported by the python programming language using a built-in package namely JSON, Its text is given in the quoted string format in which contains a key and value within the curly braces{} as same as a dictionary.

For using JSON in python, we have to import the JSON package in python script. JSON package provides several methods, among them one of the methods is dumps. This is used to convert the python tuple objects into the Java objects to perform communication between python.

Node.js is the built in JSON object to parse JSON data to JavaScript. The function parse in JSON is used to stringy the JSON object into JavaScript.

To communicate JSON data between the Node.js and python, we use http requests and responses.

Installing flask module

First we have to install the required modules to establish communication between the python and Node.js

pip install flask

Output

The following is the output of the installing the flask module.

Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/
Collecting flask
  Downloading Flask-2.2.3-py3-none-any.whl (101 kB)
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 

Installing collected packages: flask
Successfully installed flask-2.2.3

Installing request module

Now we have to install the request module to communicate between python and node js server.

npm install request-promise

Steps

Next we have to follow the steps to communicate the JSON data between python and Node.js as below.

  • First we have to import the JSON module available in python in our working environment

import json
  • Now we will create data in the dictionary format using the python and then converts the python data into json data using the dumps() function of the json module, following is the code

import json
data = {"Language":["Python","Java","C"], "Year":[2000,2004,2009]}
json_data = json.dumps(data)
print(json_data)
print(type(json_data))

When we run the above code, following output will be generated -

{"Language": ["Python", "Java", "C"], "Year": [2000, 2004, 2009]}
<class 'str'="">
  • In this step, we will convert json data into java script using parse() function available in the Node.js. The following is the code.

const json_string = '{"name": "John", "age": 30}';
const data = JSON.parse(json_string);
  • Now we have to establish a connection between python module and node js

import requests
import json
data = {"Language":["Python","Java","C"], "Year":[2000,2004,2009]}
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
response = requests.post('http://localhost:3000', data=json.dumps(data), headers=headers)
  • In this step, we will create the java script to receive the data from python to Node js.

const http = require('http');
const server = http.createServer((req, res) => {
  let data = '';
  req.on('data', chunk => {
    data += chunk;
  });
  req.on('end', () => {
    const json_data = JSON.parse(data);
    console.log(json_data);
  });
  res.end('OK');
});

server.listen(3000, () => {
  console.log('Server listening on port 3000');
}); 

Updated on: 09-Aug-2023

322 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements