Skip to content
Related Articles
Open in App
Not now

Related Articles

Node.js keyObject.export([options]) Method

Improve Article
Save Article
Like Article
  • Last Updated : 01 Feb, 2023
Improve Article
Save Article
Like Article

The export method in NodeJS allows you to create a JSON representation of an object or array. This can be useful for storing data in a file or for sending data over the network. The options argument allows you to control the output of the JSON string, such as by adding indentation.

Syntax: The syntax for using the export method is as follows:

const jsonString = object.export([options]);

The object argument is the object or array that you want to convert to JSON. The options argument is an optional object containing options for controlling the output of the JSON string.

 

Example 1: Consider the following example in which we will create an object with two properties, name, and age, and then convert it to a JSON string using the export method.

Filename: index.js

Javascript




const data = {
    name: 'ABC',
      age: 40,
};
  
const jsonString = data.export();
console.log(jsonString);


Output: 

{"name":"ABC","age":40}

Example 2: This example creates an array of objects and converts it to a JSON string using the export method. The spaces option is used to add indentation to the output JSON string.

Filename: index.js

Javascript




const data = [
    { name: 'ABC', age: 40 },
      { name: 'XYZ', age: 25 },
];
  
const jsonString = data.export({ spaces: 2 });
console.log(jsonString);


Output:

[  {    "name": "ABC",    "age": 40  },  {    "name": "XYZ",    "age": 25  }]

Reference: https://www.geeksforgeeks.org/node-js-cipher-final-method/

My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!