0

the following array list I need to get all the price one by one. this returns the full json object console.log('File data:', jsonString); but the for loop never seems to get called , it never enters it. I need to loop through a json file but its in different folder the json file is under menu folder called list.json menu-> projectName\menu\list.json the file looks like this The data:

[
  {
    "code": "ZC",
    "price": "1"
  },
  {
    "code": "ZS",
    "price": "3"
  },
  {
    "code": "VC",
    "price": "4"
  },
...]

JS:

const jsonList = fs.readFile("../menu/list.json", "utf8", (err, jsonString) => {
  if (err) {
    console.log("File read failed:", err);
    return;
  }
  console.log("File data:", jsonString);
  console.log("File data:", jsonString.url);
  for (var key in jsonString) {
    if (jsonString.hasOwnProperty(key)) {
      console.log("===>", jsonString[key].price);
    }
    return jsonString;
  }
});
ROOT
  • 11,363
  • 5
  • 30
  • 45
benji_r
  • 535
  • 2
  • 16
  • 34
  • I think you need to convert your jsonList using JSON.parse method something like jsonList = JSON.parse(jsonList) just before your for loop – Syam May 05 '20 at 05:29
  • Not sure of the issue with your code but a simpler approach might be jsonList.forEach { item => console.log("==>" + item.price } – Chris May 05 '20 at 05:32

2 Answers2

1

I think you need to loop in the callback as it is async and so jsonList is not the object you expect when you access it. See Get data from fs.readFile

Chris
  • 1,644
  • 1
  • 11
  • 15
  • this is giving me undefiend console.log('====> ', jsonList); – benji_r May 05 '20 at 05:40
  • I think that makes sense based on async - what if you put console.log(jsonString) instead of return jsonString. Does that show it as you expect at that point – Chris May 05 '20 at 05:43
  • console.log('File data:', jsonString); this gives me the correct stuff, however moving the forloop into the fs .readfile returns undefined – benji_r May 05 '20 at 05:47
  • for (var key in jsonString) { console.log('byeeeee') if (jsonString.hasOwnProperty(key)) { console.log('===>',jsonString[key].price); } } – benji_r May 05 '20 at 05:49
  • Possibly doesn't explain undefined but as Syam mentioned you need JSON.parse(jsonString) so instead of return jsonString, JSON.parse(jsonString).forEach { item => console.log(item.price) } – Chris May 05 '20 at 05:56
  • Can you copy your updated code into a comment and let us know what is undefined – Chris May 05 '20 at 05:57
  • Updating the code in the question is a really bad idea because now the answers that are posted make no sense. You should append an update to your question ie leave original as is, and then add a bit to say you updated code and it still doesn't work - here is the updated version . – Chris May 05 '20 at 06:14
  • const jsonList = JSON.parse(jsonString); that is what I needed . thank you somuch for your time – benji_r May 05 '20 at 06:23
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/213133/discussion-between-benji-r-and-chris). – benji_r May 05 '20 at 06:24
1

There are two ways to fix the issue you are facing, one is to have your code run inside the callback:

const jsonList = fs.readFile("../menu/list.json", "utf8", (err, jsonString) => {
  if (err) {
    console.log("File read failed:", err);
    return;
  }
  console.log("File data:", jsonString);
  for (var key in JSON.parse(jsonString)) {
    if (jsonList.hasOwnProperty(key)) {
      console.log("===>", jsonList[key].price); // This is never called
    }
  }
});

or by using sync function to read file:

const jsonString = fs.readFileSync("../menu/list.json", "utf8");
console.log("File data:", jsonString);
const jsonList = JSON.parse(jsonString);
for (var key in jsonList) {
  if (jsonList.hasOwnProperty(key)) {
    console.log("===>", jsonList[key].price); // This is never called
  }
}
ROOT
  • 11,363
  • 5
  • 30
  • 45