jprichardson / node-jsonfile

Easily read/write JSON files.
MIT License
1.2k stars 321 forks source link

obj.data ? How do I read specific element in json file? #54

Closed lazarusvc closed 8 years ago

lazarusvc commented 8 years ago

If I have a Json file say:

{ "data": { "points" : "1", "name": "Jones" } }

How do I read "points" value?

jprichardson commented 8 years ago
var jsonfile = require('jsonfile')
var file = 'myfile.json'

console.log(jsonfile.readFileSync(file).data.points)
lazarusvc commented 8 years ago

Okay but what if it's in the format :

{ "data": [ { "email": " ", "products": [ ], "username": " " }, { "email": " ", "products": [ { "actual_food_amount": "15", "amount": "20", "drink": "Local Drink ", "food": "Dasheen meal w/ codfish", "id": 2, "location": "Orchard Restaurant" } ] } }

and I want to read "food"