Practical 2 JASON
Practical 2 JASON
Practical-2:
1. Create a Jason object and display the same using node.js in the terminal.
2. Create a Node.js script to read and display the content of the external Jason
file.
3. Create and display multi-dimensional Jason Arrays, also demonstrate
Accessing individual elements of the multi-dimensional Jason Arrays.
4. simple web-based application example using JavaScript and JSON, you can
create a basic HTML page with JavaScript to manipulate JSON data. In this
example, let's create a simple to-do list application where tasks are stored in
a JSON array. Users can add tasks, mark them as completed, and remove
them from the list.
1. To display the contents of a JSON object in Node.js, you can use the console.log
function to print the object to the console. Here's a simple example:
{
"name": "John Doe",
"age": 25,
"city": "Example City"
}
// Node.js fs module, which provides an API for interacting with the file system.
const fs = require('fs');
try {
// Parse the JSON data
const jsonObject = JSON.parse(data);
The fs.readFile function is asynchronous and takes three parameters: the file path
(filePath), the encoding ('utf8' for text-based files), and a callback function. The
callback function is executed once the file is read. If there is an error, it prints an
error message. If there is no error, the file content is available in the data
parameter.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Todo List App</title>
<style>
ul {
list-style-type: none;
padding: 0;
}
li {
margin: 5px 0;
}
.completed {
text-decoration: line-through;
}
</style>
2CS201-Full Stack Web Development
</head>
<body>
<ul id="taskList"></ul>
<script>
// Sample JSON array for tasks (initializes a JavaScript array named tasks)
let tasks = [
{ id: 1, text: 'Complete assignment', completed: false },
{ id: 2, text: 'Read a book', completed: true },
{ id: 3, text: 'Go for a run', completed: false }
];
tasks.forEach(task => {
const li = document.createElement('li');
li.textContent = task.text;
if (task.completed) {
li.classList.add('completed');
}
tasks.push(newTask);
displayTasks();
taskInput.value = '';
}
}
2CS201-Full Stack Web Development
</body>
</html>
width=device-width:
This part sets the width of the viewport to the width of the device. In other words, it
ensures that the width of the web page is equal to the width of the device's screen.