2. JSON Introduction
JSON stands for JavaScript Object Notation.
It is a format for structuring data. This format is used by different web
applications to communicate with each other.
JSON is the replacement of the XML data exchange format in JSON. It is
easy to struct the data compare to XML.
It supports data structures like arrays and objects and the JSON documents
that are rapidly executed on the server.
It is also a Language-Independent format that is derived from JavaScript.
The official media type for the JSON is application/json and to save those
file .json extension.
3. Features of JSON:
Easy to understand: JSON is easy to read and write.
Format: It is a text-based interchange format. It can store any kind of data in an
array of video, audio, and image anything that you required.
Support: It is light-weighted and supported by almost every language and OS. It
has a wide range of support for the browsers approx each browser supported by
JSON.
Dependency: It is an Independent language that is text-based. It is much faster
compared to other text-based structured data.
4. • The JSON syntax is derived from JavaScript object notation syntax, but the
JSON format is text only. Code for reading and generating JSON data can be
written in any programming language.This JSON syntax defines an employees
object: an array of 3 employee records (objects):
• JSON Example
• {
"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]
}
The JSON Format Evaluates to JavaScript Objects.The JSON format is
syntactically identical to the code for creating JavaScript objects.
• Because of this similarity, a JavaScript program can easily convert JSON data
into native JavaScript objects.
5. JSON Syntax Rules:
1.Data is in name/value pairs
2.Data is separated by commas
3.Curly braces hold objects
4.Square brackets hold arrays
{ "Courses": [
{ "Name" : "Java Foundation",
"Created by" : "Data Analyst",
"Content" : [ "Java Core", "JSP","Servlets", "Collections" ]
},
{ "Name" : "Data Structures",
"also known as" : "Interview Preparation Course",
"Topics" : [ "Trees", "Graphs", "Maps" ] }]
}
6. THREE METHODS USED TO PERFORM TASK IN
JSON
• JavaScript Object Notation means that a script (executable) file which is made of text in a programming language, is
used to store and transfer the data. The data can be easily processed if it is in JSON format. The JSON file which we
import can be either from the local server or a web API. There are basically three methods used to perform this task.
JavaScript fetch() Method: Request data from a server, this request can be of any type of API that returns the data in
JSON or XML.
NodeJS require() Function: Invokes that require() function with a file path as the function’s only argument, Node
goes through the following sequence of steps:
Resolving and Loading
Wrapping
Execution
Returning Exports
Caching
JavaScript ES6 import Module: You can import a variable using the import keyword. You can specify one of all the
members that you want to import from a JavaScript file.
7. WORKING WITH JSON
• JSON is a format for storing and transporting data.JSON is often used when data is sent
from a server to a web page.
• The package.json file is the heart of any Node project. It records important metadata about
a project which is required before publishing to NPM, and also defines functional
attributes of a project that npm uses to install dependencies, run scripts, and identify the
entry point to our package.
• What is JSON?
JSON stands for JavaScript Object Notation
JSON is a lightweight data interchange format
JSON is language independent *
JSON is "self-describing" and easy to understand
8. • STRING TO JSON OBJECT:
This is very much easier and straight forward as below:
var jsonString = "{"key":"value"}";
var jsonObj = JSON.parse(jsonString);
console.log(jsonObj.key);
As you can see, we are using the built-in global JSON Object to parse a string
which has JSON Data. Also, it might be good idea to use “.trim()” method on
the string, if you think there might be some chance of extra space etc in the
JSON string. Otherwise, it won’t get parsed and you will face an unexpected
error.
9. JSON Object To String:
As like the previous case, we can use the same global object’s ‘stringify’
method to convert a given json to string data. This can be done easily as
like below:
var jsonObj = {'key':'value'};
console.log(JSON.stringify(jsonObj));
10. Treat User Defined Class Instance To JSON String:
• If you are writing JavaScript OOP style and want to convert an object instance to
JSON like string(with its attributes name/value as key/value), You still can use the
same JSON object to string approach as below:
function MyClass(){
this.a = 'some value';
this.b = {
'key': 'another json structure'
};
}
var instance = new MyClass();
console.log(JSON.stringify(instance));
• However, you will need to be careful that you are declaring properties properly
instead of declaring them as local variable. This stack-overflow thread might also
help in understanding the differences easily.
11. Add New Element To Existing JSON Object:
Say, you have an existing json object, which you want to modify to add
new key/value pair(s). You can do that using either of the two ways as
below:
var myJson = {'key':'value'};
//new element
myJson.key2 = 'value2';
//or
myJson[key3] = 'value3';
12. Read JSON From File System In NodeJS:
• I found a solution, which shows example with file system support of
nodejs(fs module). But, I don’t really see any meaning of that at all, as
we can simply do the same thing by:
var jsonObj = require("./path/to/myjsonfile.json");
• Here, NodeJS automatically read the file, parse the content to a JSON
object and assigns that to the left hand side variable.
13. Delete An Element From A JSON Object:
Well, to delete an element from a JSON object, it can be done by using
the ‘delete’ keyword. An example is given below:
var myJson = {'key':'value'};
delete myJson['key'];
14. Converting a JSON Text to a JavaScript Object
• A common use of JSON is to read data from a web server, and display the data in a web page.
• For simplicity, this can be demonstrated using a string as input.
• First, create a JavaScript string containing JSON syntax:
• let text = '{ "employees" : [' +
'{ "firstName":"John" , "lastName":"Doe" },' +
'{ "firstName":"Anna" , "lastName":"Smith" },' +
'{ "firstName":"Peter" , "lastName":"Jones" } ]}';
• Then, use the JavaScript built-in function JSON.parse() to convert the string into a JavaScript object:
• const obj = JSON.parse(text);
• Finally, use the new JavaScript object in your page:Example
• <p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
obj.employees[1].firstName + " " + obj.employees[1].lastName;
</script>
15. Iterate Over A JSON Object:
Sometimes you will might need to traverse through each elements of the
JSON object. This can be done in a for loop easily as like below:
var myJson = {'key':'value', 'key2':'value2'};
for(var myKey in myJson) {
console.log("key:"+myKey+", value:"+myJson[myKey]);
}
However, the above code could give you error in case the value itself is
a JSON object. So, you will might want to check whether the value is
itself json or not and handle it thereby.
16. Check Key Existence:
If at some point we need to check whether a json object have a specific
key, we can check that with below approach:
var myJson = {'key':'value', 'key2':'value2'};
if(myJson.hasOwnProperty('key2')){
//do something if the key exist
17. Pretty Print JSON Object:
• In debugging, we alway like to print data to console to verify if its
OK. If you are trying to see if a large JSON has something you are
expecting, then its very hard to locate if its printed in flat structure.
• In Such cases, what you need is pretty printing the JSON object. Here
is the javascript code snippet that will do the trick:
JSON.stringify(myObj, null, 2);
• Same applies if you are trying to write the json object in a file with
pretty printed format.
18. Synchronous vs Asynchronous
• Every method in the fs module has synchronous as well as
asynchronous forms.
• Asynchronous methods take the last parameter as the completion
function callback and the first parameter of the callback function as
error.
• It is better to use an asynchronous method instead of a synchronous
method, as the former never blocks a program during its execution,
whereas the second one does.
19. SYNCHRONOUS METHODS:
• Synchronous functions block the execution of the program until the file operation is performed. These
functions are also called blocking functions. The synchronous methods have File Descriptor as the last
argument.
• File Descriptor is a reference to opened files. It is a number or a reference id to the file returned after
opening the file using fs.open() method of the fs module. All asynchronous methods can perform
synchronously just by appending “Sync” to the function name.
• Some of the synchronous methods of fs module in NodeJS are:
fs.readFileSync()
fs.renameSync()
fs.writeSync()
fs.writeFileSync()
fs.fsyncSync()
fs.appendFileSync()
fs.statSync()
20. ASYNCHRONOUS METHODS:
• Asynchronous functions do not block the execution of the program and each command is executed after
the previous command even if the previous command has not computed the result. The previous
command runs in the background and loads the result once it has finished processing. Thus, these
functions are called non-blocking functions. They take a callback function as the last parameter.
• Asynchronous functions are generally preferred over synchronous functions as they do not block the
execution of the program whereas synchronous functions block the execution of the program until it has
finished processing.
• Some of the asynchronous methods of fs module in NodeJS are:
fs.readFile()
fs.rename()
fs.write()
fs.writeFile()
fs.fsync()
fs.appendFile()
21. DIFFERENCE BETWEEN ASYNCHRONOUS AND
SYNCHRONOUS METHODS
Sr.no Synchronous methods Asynchronous methods
1. Synchronous functions are called blocking functions Asynchronous functions are called non-blocking functions.
2.
It blocks the execution of the program until the file operation
has finished processing.
It does not block the execution of the program.
3. These functions take File Descriptor as the last argument. These functions take a callback function as the last argument.
4.
Examples: fs.readFileSync(), fs.appendFileSync(),
fs.writeFileSync() etc.
Examples: fs.readFile(), fs.appendFile(), fs.writeFile(), fs.stat()
etc.
22. Buffer data
• Pure JavaScript is great with Unicode encoded strings, but it does not
handle binary data very well.
• It is not problematic when we perform an operation on data at browser
level but at the time of dealing with TCP stream and performing a
read-write operation on the file system is required to deal with pure
binary data.
• To satisfy this need Node.js use Buffer, So in this article, we are going
to know about buffer in Node.js.
23. Buffers in Node.js:
• The Buffer class in Node.js is used to perform operations on raw
binary data. Generally, Buffer refers to the particular memory location
in memory.
• Buffer and array have some similarities, but the difference is array can
be any type, and it can be resizable. Buffers only deal with binary data,
and it can not be resizable.
• Each integer in a buffer represents a byte. console.log() function is
used to print the Buffer instance
24. Methods to perform the operations on Buffer:
No Method Description
1 Buffer.alloc(size) It creates a buffer and allocates size to it.
2 Buffer.from(initialization) It initializes the buffer with given data.
3 Buffer.write(data) It writes the data on the buffer.
4 toString() It read data from the buffer and returned it.
5 Buffer.isBuffer(object) It checks whether the object is a buffer or not.
6 Buffer.length It returns the length of the buffer.
7 Buffer.copy(buffer,subsection size) It copies data from one buffer to another.
8 Buffer.slice(start, end=buffer.length) It returns the subsection of data stored in a buffer.
9 Buffer.concat([buffer,buffer]) It concatenates two buffers.
25. Stream data
What are Streams?
Streams are objects that let you read data from a source or write data to a destination
in continuous fashion. In Node.js, there are four types of streams −
Readable − Stream which is used for read operation.
Writable − Stream which is used for write operation.
Duplex − Stream which can be used for both read and write operation.
Transform − A type of duplex stream where the output is computed based on input.
26. Each type of Stream is an EventEmitter instance and throws several events at
different instance of times. For example, some of the commonly used events
are −
data − This event is fired when there is data is available to read.
end − This event is fired when there is no more data to read.
error − This event is fired when there is any error receiving or writing data.
finish − This event is fired when all the data has been flushed to underlying
system.
27. Readable Stream Properties and Methods
Method Description
isPaused() Returns true if the state of the readable stream is paused, otherwise false
pause() Pauses the readable stream
pipe() Turns the readable stream into the specified writable stream
read() Returns a specified part of the readable stream
resume() Resumes a paused stream
setEncoding() Sets the character encoding of the readable stream
unpipe() Stops turning a readable stream into a writable stream, caused by the pipe() method
unshift() Pushes some specified data back into the internal buffer
wrap() Helps reading streams made by older Node.js versions
28. Writable Stream Properties and Methods
Method Description
cork() Stops the writable stream and all written data will be buffered in memory
end() Ends the writable stream
setDefaultEncoding() Sets the encoding for the writable stream
uncork() Flushes all data that has been buffered since the cork() method was called
write() Writes data to the stream
29. This tutorial provides a basic understanding of the commonly used
operations on Streams.
Reading from a Stream
Create a text file named input.txt having the following content −
Tutorials Point is giving self learning content
to teach the world in simple and easy way!!!!!
30. This provides a basic understanding of the commonly used operations on Streams.
Reading from a StreamCreate a text file named input.txt having the following content −
var fs = require("fs");
var data = '';
// Create a readable stream
var readerStream = fs.createReadStream('input.txt');
// Set the encoding to be utf8.
readerStream.setEncoding('UTF8');
// Handle stream events --> data, end, and error
readerStream.on('data', function(chunk) {
data += chunk;
});
readerStream.on('end',function() {
console.log(data);
});
readerStream.on('error', function(err) {
console.log(err.stack);
});
31. Writing to a Stream
var fs = require("fs");
var data = 'Simply Easy Learning';
// Create a writable stream
var writerStream = fs.createWriteStream('output.txt');
// Write the data to stream with encoding to be utf8
writerStream.write(data,'UTF8');
// Mark the end of file
writerStream.end();
// Handle stream events --> finish, and error
writerStream.on('finish', function() {
console.log("Write completed.");
});
writerStream.on('error', function(err) {
console.log(err.stack);
});
console.log("Program Ended");
32. Now run the main.js to see the result −
$ node main.js
Verify the Output.
Program Ended
Write completed.
Now open output.txt created in your current directory; it should contain
the following −
Simply Easy Learning
33. COMPRESSION DECOMPRESSION FILE
What is compression and decompression?
• Compression reduces the size of an application or document for storage or transmission.
Compressed files are smaller, download faster, and easier to transport.
• Decompression or expansion restores the document or application to its original size
• Typically, a device that performs data compression is referred to as an encoder, and one that
performs the reversal of the process (decompression) as a decoder.
• To utilize these resources efficiently, the data is often required to be compressed, i.e., reduced to a
smaller size without losing any or losing minimal information
• The process of reducing the size of a data file is often referred to as data compression. In the
context of data transmission, it is called source coding: encoding is done at the source of the data
before it is stored or transmitted.
Advantages Of Compression
Reduces the disk space occupied by the file.
Reading and Writing of files can be done quickly.
Increases the speed of transferring files through the internet and other networks.
34. Piping the Streams
• Piping is a mechanism where we provide the output of one stream as
the input to another stream.
• It is normally used to get data from one stream and to pass the output
of that stream to another stream.
• There is no limit on piping operations.
• Now we'll show a piping example for reading from one file and
writing it to another file.
35. var fs = require("fs");
// Create a readable stream
var readerStream = fs.createReadStream('input.txt');
// Create a writable stream
var writerStream = fs.createWriteStream('output.txt');
// Pipe the read and write operations
// read input.txt and write data to output.txt
readerStream.pipe(writerStream);
console.log("Program Ended");
36. Now run the main.js to see the result −
$ node main.js
Verify the Output.
Program Ended
Open output.txt created in your current directory; it should contain the
following −
Tutorials Point is giving self learning content
to teach the world in simple and easy way!!!!!
37. var fs = require("fs");
var zlib = require('zlib');
// Compress the file input.txt to input.txt.gz
fs.createReadStream('input.txt')
.pipe(zlib.createGzip())
.pipe(fs.createWriteStream('input.txt.gz'));
console.log("File Compressed.");
Now run the main.js to see the result −
$ node main.js
Verify the Output.
File Compressed.
38. • Chaining is a mechanism to connect the output of one stream to
another stream and create a chain of multiple stream operations. It is
normally used with piping operations.
• Now we'll use piping and chaining to first compress a file and then
decompress the same.
39. You will find that input.txt has been compressed and it created a file input.txt.gz in the
current directory. Now let's try to decompress the same file using the following code −
var fs = require("fs");
var zlib = require('zlib');
// Decompress the file input.txt.gz to input.txt
fs.createReadStream('input.txt.gz')
.pipe(zlib.createGunzip())
.pipe(fs.createWriteStream('input.txt'));
console.log("File Decompressed.");
Now run the main.js to see the result −
$ node main.js
Verify the Output.
File Decompressed.
40. Files
• The Node.js file system module allows you to work with the file system on your computer.
• To include the File System module, use the require() method:
• var fs = require('fs');
• Common use for the File System module:
1. Read files
2. Create files
3. Update files
4. Delete files
5. Rename files
41. Create Files
The File System module has methods for creating new files:
fs.appendFile()
fs.open()
fs.writeFile()
The fs.appendFile() method appends specified content to a file. If the file does not exist, the file will be created:
Example:
Create a new file using the appendFile() method:
var fs = require('fs');
fs.appendFile('mynewfile1.txt', 'Hello content!', function (err) {
if (err) throw err;
console.log('Saved!');
});
42. The fs.open() method takes a "flag" as the second argument, if the flag is "w"
for "writing", the specified file is opened for writing. If the file does not exist,
an empty file is created:
Example
Create a new, empty file using the open() method:
var fs = require('fs');
fs.open('mynewfile2.txt', 'w', function (err, file) {
if (err) throw err;
console.log('Saved!');
});
43. The fs.writeFile() method replaces the specified file and content if it exists. If
the file does not exist, a new file, containing the specified content, will be
created:
Example
Create a new file using the writeFile() method:
var fs = require('fs');
fs.writeFile('mynewfile3.txt', 'Hello content!', function (err) {
if (err) throw err;
console.log('Saved!');
});
44. Update Files
The File System module has methods for updating files:
fs.appendFile()
fs.writeFile()
The fs.appendFile() method appends the specified content at the end of the specified file:
Example
Append "This is my text." to the end of the file "mynewfile1.txt":
var fs = require('fs');
fs.appendFile('mynewfile1.txt', ' This is my text.', function (err) {
if (err) throw err;
console.log('Updated!');
});