Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Read json file & print only specific rows

I have below json data file & want to do below things:

  1. Read json file & check row count (this is completed)
  2. I have setup maxRowCnt=2 so I need to print only 2 rows from json file

Sample Json File:

[
  {
    "Name": "User1",
    "primaryRegion": "US"
  },
  {
    "Name": "user2",
    "primaryRegion": "US"
  },
  {
    "Name": "user3",
    "primaryRegion": "US"
  },
  {
    "Name": "user4",
    "primaryRegion": "US"
  },
  {
    "Name": "user5",
    "primaryRegion": "US"
  },
  {
    "Name": "user6",
    "primaryRegion": "US"
  },
  {
    "Name": "user7",
    "primaryRegion": "US"
  }
]

#2 I tried to print json data but getting data as follows:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Json Data---"[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]"

Code:

const fs = require("fs");

let jsonData = JSON.parse(fs.readFileSync('./test/sampleData1.json', 'utf8'));

console.log('Debug1---'+typeof(jsonData));

let jsonRowCount = jsonData.length;
let maxRowCount = 2;

console.log("JSON ROW COUNT Printed here---"+jsonRowCount);

let jsonData1 = jsonData.toString().split('\n');

console.log('Debug2---'+typeof(jsonData1));

for(let i=1; i <= maxRowCount; i++){
  
  console.log("i Value Print---"+i);

  console.log("Json Data---"+JSON.stringify(jsonData1);
  
}

Output Should be:

  [{
    "Name": "User1",
    "primaryRegion": "US"
  },
  {
    "Name": "user2",
    "primaryRegion": "US"
  }]

>Solution :

From what i see, i think to print only the first three rows from the JSON file in Node.js, you don’t need to split the JSON data by newline characters because the JSON data is already an array of objects. You can directly access the elements of this array. Try and refactor your code like this to achieve the desired output

javascript

const fs = require('fs');

// Reading and parsing the JSON file
let jsonData = JSON.parse(fs.readFileSync('./test/sampleData1.json', 'utf8'));

// Define the maximum number of rows to print
let maxRowCount = 3;

console.log("JSON ROW COUNT Printed here---" + jsonData.length);

// Looping through the first maxRowCount elements of the JSON array
for (let i = 0; i < maxRowCount; i++) {
    console.log("Row " + (i + 1) + " Data---" + JSON.stringify(jsonData[i], null, 2));
}

i hope this helps

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading