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;
  }
});
2
  • I think you need to convert your jsonList using JSON.parse method something like jsonList = JSON.parse(jsonList) just before your for loop Commented May 5, 2020 at 5:29
  • Not sure of the issue with your code but a simpler approach might be jsonList.forEach { item => console.log("==>" + item.price } Commented May 5, 2020 at 5:32

2 Answers 2

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
  }
}
Sign up to request clarification or add additional context in comments.

7 Comments

the sync function returns undefined, I dont get it
console.log("File data:", jsonString); this returns the json object but the .price is undefined :(
both are undefined
@benji_r, can you post the file list.json structure?
that was it thank you so much const jsonList = JSON.parse(jsonString);
|
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

9 Comments

this is giving me undefiend console.log('====> ', jsonList);
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
console.log('File data:', jsonString); this gives me the correct stuff, however moving the forloop into the fs .readfile returns undefined
for (var key in jsonString) { console.log('byeeeee') if (jsonString.hasOwnProperty(key)) { console.log('===>',jsonString[key].price); } }
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) }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.