Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»Nodejs»How To Parse JSON in Node.js

    How To Parse JSON in Node.js

    RahulBy RahulFebruary 18, 20172 Mins ReadUpdated:June 28, 2022

    JSON, or JavaScript Object Notation, is a lightweight data-interchange format that is easy for humans to read and write. Node.js has built-in support for parsing JSON files, making it a breeze to work with JSON data. To parse a JSON file in Node.js, you can use the built-in JSON module. The JSON module provides an efficient way to parse and stringify JSON data. It also provides a number of helpful methods for working with JSON data.

    This tutorial will help you to read JSON file using Node.js uses readFile and readFileSync functions of the jsonfile module.

    Installing Node Module

    For this tutorial, we are using jsonfile npm module. So first you need to install jsonfile module on your system

    npm install jsonfile --save 
    

    Next create a sample json file for running few examples. You can use your own json file.

    nano employee.json 
    

    Add the follwoing snippte:

    [
      {
        "emp_id" : "101",
        "emp_name" : "Mike",
        "emp_addr" : "123 California, USA",
        "designation" : "Editor"
      },
      {
        "emp_id" : "102",
        "emp_name" : "Jacob",
        "emp_addr" : "456 Log Angelis, USA",
        "designation" : "Chief Editor"
      }
    ]
    

    Save file content and close it.

    Next, we will run a few examples to read JSON file created above.

    Option #1: Read JSON File with Nodejs

    In the above step, I have created a sample JSON file. Now create ReadJsonFile.js and add the following content. You need to change employee.json with your JSON file name.

    Filename: ReadJsonFile.js

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    var jsonFile = require('jsonfile')
    var fileName = 'employee.json'
     
    jsonFile.readFile(fileName, function(err, jsonData) {
      if (err) throw err;
      for (var i = 0; i < jsonData.length; ++i) {
     
        console.log("Emp ID: "+jsonData[i].emp_id);
        console.log("Emp Name: "+jsonData[i].emp_name);
        console.log("Emp Address: "+jsonData[i].emp_addr);
        console.log("Designation: "+jsonData[i].designation);
        console.log("----------------------------------");
      }
    });

    Now run the nodejs script using following command.

    node ReadJsonFile.js 
    
    Emp ID: 101
    Emp Name: Mike
    Emp Address: 123 California, USA
    Designation: Editor
    ----------------------------------
    Emp ID: 102
    Emp Name: Jacob
    Emp Address: 456 Log Angelis, USA
    Designation: Chief Editor
    ----------------------------------
    

    Option #2: Read JSON File (readFileSync)

    Alternatively, you can use readFileSync function to read json file content. Create a ReadJsonFileSync.js file with following content. You can read here about the differences of readFile and readFileSync function in Jode.js.

    Filename: ReadJsonFileSync.js

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    var jsonFile = require('jsonfile')
    var fileName = 'employee.json'
     
    var jsonData = jsonFile.readFileSync(fileName);
     
    for (var i = 0; i < jsonData.length; ++i) {
     
    console.log("Emp ID : "+jsonData[i].emp_id);
    console.log("Emp Name : "+jsonData[i].emp_name);
    console.log("Emp Address : "+jsonData[i].emp_addr);
    console.log("Designation : "+jsonData[i].designation);
    console.log("----------------------------------");
    }

    Now run the nodejs script using following command.

    node ReadJsonFileSync.js 
    
    Emp ID: 101
    Emp Name: Mike
    Emp Address: 123 California, USA
    Designation: Editor
    ----------------------------------
    Emp ID: 102
    Emp Name: Jacob
    Emp Address: 456 Log Angelis, USA
    Designation: Chief Editor
    ----------------------------------
    
    json node NodeJs
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Use SSH to Connect to a Remote Server
    Next Article How to Get Current Date and Time in PHP

    Related Posts

    How To Install Node.js on Ubuntu 22.04

    Updated:May 27, 20223 Mins Read

    How To Install NVM on Ubuntu 22.04

    Updated:April 16, 20223 Mins Read

    How To Install NVM on Windows

    Updated:April 16, 20223 Mins Read

    How To Install Node.Js on Debian 11

    Updated:February 11, 20226 Mins Read

    How To Install NVM on Debian 11

    Updated:August 31, 20213 Mins Read

    How to Read, Write & Parse JSON in Python

    3 Mins Read

    3 Comments

    1. Vik on May 27, 2018 6:00 am

      How can we break this down further and create routes to pull only what we want from the .json file?

      Reply
    2. Vishnu on December 8, 2017 7:06 am

      Very helpful information for learners like me, I would thankful for this blog.

      Reply
    3. sarika on April 10, 2017 6:49 am

      Thanks for this post! I’m still a learner on this technology,this info seems to be really helpful. Thanks again!

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • What is the /etc/aliases file
    • What is the /etc/nsswitch.conf file in Linux
    • How to Install Ionic Framework on Ubuntu 22.04
    • What is the /etc/hosts file in Linux
    • How to Install Angular CLI on Ubuntu 22.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.