Open In App

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

Last Updated : 01 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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/


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads