2

Hi I got the following code in Node.js

fs.readFile(file, function(err, obj) {
    obj.data1.forEach(function(element) {
        console.log (element.key, element.key1);
    });
})

I am trying to show the key and the value of all the in the following json format:

{
    "data1": {
        "key": "iohiohio",
        "key1": "jhuihuj"
    },
    "data2": {
        "key4": "hoih",
        "key5": "kjhi"
   }
}

So i want the result to be like :

key1:jhuihuj , key4: hoih

and be shown on a html/ejs file.

momori14
  • 169
  • 1
  • 2
  • 14

2 Answers2

2

the problem is that obj is returned from fs.readFile as a buffer or a string (if utf-8 formatting is provided).

In order to convert the string or the buffer to an actual object You have to use JSON.parse() method.

Here is a commented code to understand what to do step by step:

var fs = require("fs");
fs.readFile("./file.json",  "utf-8", function(err, obj) {
    // print your json file to the screen
    console.log(obj);

    // parse the obj string and convert it to an actual object
    obj = JSON.parse(obj);

    // print the properties of obj.data1 as "key : value"
    for (k in obj.data1) {
        console.log(k, ":", obj.data1[k]);
    }
})

The console result:

D:\workspace\projects\node>node server
{
    "data1": {
        "key": "iohiohio",
        "key1": "jhuihuj"
    },
    "data2": {
        "key4": "hoih",
        "key5": "kjhi"
   }
}
key : iohiohio
key1 : jhuihuj
Abdelaziz Mokhnache
  • 4,269
  • 3
  • 25
  • 35
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/133087/discussion-on-answer-by-abdelaziz-mokhnache-i-cant-read-a-json-file-with-node-j). – Bhargav Rao Jan 13 '17 at 13:58
0

What's the point of having your values inside an array? It's redundant because you have a single object in them. You could remove the square brackets and just have this:

{
    "data1": {
        "key": "iohiohio",
        "key1": "jhuihuj"
    },
    "data2": {
        "key4": "hoih",
        "key5": "kjhi"
    }
}

Then, you would loop it like this:

fs.readFile(file, function(err, obj) {
    for (var k in obj.data1) {
        console.log(k + ": " + obj.data1[k]);
    }
});

That would print only the keys in data1, though! To print everything, you should use:

fs.readFile(file, function(err, obj) {
    try {
        obj = JSON.parse(obj);
    } catch(e) {
        console.log("Error while parsing");
        return;
    }

    for (var k in obj) {
        for (var k2 in obj[k]) {
             console.log(k + " - " + k2 + ": " + obj[k][k2]);
        }
    }
});

// Result
// data1 - key: iohiohio
// data1 - key1: jhuihuj
// data2 - key4: hoih
// data2 - key5: kjhi

Edit: you should parse obj. It's also better to put it in a try catch block in case the JSON is wrong and couldn't be parsed.

You can also check this, you could load the json with require:

var obj = require("./jsonfile");
for (var k in obj) {
    for (var k2 in obj[k]) {
         console.log(k + " - " + k2 + ": " + obj[k][k2]);
    }
}
Community
  • 1
  • 1
dodov
  • 5,206
  • 3
  • 34
  • 65