DEV Community

Cover image for How to read multiple value from web
Stillnoturdad
Stillnoturdad

Posted on

How to read multiple value from web

In Node.js, working with data often involves reading from files and inserting those data into databases. Below is how to read JSON data from a file, process it, and format it for a SQL INSERT statement:

  1. Using fs.readFile method to read the contents of a file
  2. Use JSON.parse to convert the JSON string into an object
  3. Iterates over each element in the JSON array using .map(). Inside the .map() function, specific fields from each JSON object are restructured.

Example:
const dataEntries = JSON.parse(await fs.readFile('./filename.json', 'utf-8'))
.map(element => {
const { value1, value2 } = element;
return
('${value1}', '${value2}');
});

Top comments (0)