Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

515
Views
How to retrieve object from JSON in nodejs?

Having this code:

const fs = require('fs')

const file = 'books.json';

class Book{
  constructor(code) {
    this._code = code;
  }

  get code() {
    return this._code;
  }

  set code(value) {
    this._code = value;
  }
}

async function writeBooks(){
  const data = JSON.stringify([new Book('c1'), new Book('c2')]);
  await fs.promises.writeFile(file, data, 'utf8');
}

async function getBook(code){
  try{
    const data = await fs.promises.readFile(file);
    const array = JSON.parse(data);
    return array.find(b => b.code === code);
  } catch (err){
    console.log(err)
  }
}

writeBooks();
getBook('c1').then(b => console.log(b));

I am getting undefined (instead of the expecting book object).

  1. How to get the object (the above problem)

  2. If async function always returns promise, how can I then return object for the client, instead of him having to call then() from the getBook(code)?

  3. do I need to await for the fs.promises.writeFile()? as I am doing in writeBooks()? As fas as I understand the async/await now, is that the return value from await function is the data or error. But since the writeFile() does not returns anything, or error at most (as opposed to readFile()) why would I want to await for no data?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Actually the root of problem is not about async/awaits or promises. The problem is trying to write an array to a json file. If you write your json data like the code snippet below (as a key-value pair), your problem is solved.

{"1": [new Book('c1').code, new Book('c2').code]} //as a key-value pair

const fs = require('fs')
const file = 'books.json';
class Book{
  constructor(code) {
    this._code = code;
  }
  get code() {
    return this._code;
  }
  set code(value) {
    this._code = value;
  }
}
async function writeBooks(){
  const data = JSON.stringify({"1": [new Book('c1').code, new Book('c2').code]});
  await fs.promises.writeFile(file, data, 'utf8');
}
async function getBook(code){
  try{
    const data = await fs.promises.readFile(file);
    const dataOnJsonFile = JSON.parse(data);
    return dataOnJsonFile["1"];
  } catch (err){
    console.log(err)
  }
}
writeBooks();
getBook('c1').then(b => console.log(b));

about 4 years ago · Juan Pablo Isaza Report

0

  1. The above problem is that the Books returned from JSON.parse have only data, not methods, and thus I cannot get the code via get code(){}, but only as public parameter of class Book as book._code, which however breaks encapsulation (convetion is that _[propery] is private, and there should be appropriate getters/setters). So I made the properties public (and broke encapsulation), because I still don't know, how to assign methods to object created from JSON.

  2. No, the result of async is always Promise. You cannot unwrap it inside async, the client will always have to unwrap it. (so await fs.promises.WriteFile() will unwrap it, but then immediately wrap it back, before async function returns.

  3. as explained above.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!