0

JSON data : {

"8500500561":{
         "food":"400",
         "cloth":"44",
         "travel":"44"
         }

}

I'll get the key value to access the JSON data dynamically

Node.js file :

 const fs  = requrie('fs');
 let rawdata = fs.readFileSync('data.json');
 let mydata = JSON.parse(rawdata);
 mob = '8500500561';
 console.log(mydata.mob.food);

but when I use this I'm getting an error : TypeError: Cannot read property 'food' of undefined

3

1 Answer 1

1

MadWard is correct in his comment. You would to structure it like this. You do have some typos in your example you are probably aware of but just pointing them out in case.

const fs  = requrie('fs'); //should be require
let rawdata = fs.readFileSync('data.json');
let mydata = JSON.parse(rawdata);
mob = '8500500561'; //850050051 in dataFile
console.log(mydata[mob].food);
Sign up to request clarification or add additional context in comments.

Comments

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.