0

When I require data.json in node Why node doesn't serialise info object in following json structure?

data.json

{
 "mailOptions": {
        "from": "from me",
        "service": "gmail",
        "auth": {
            "user": "username",
            "pass": "password",
           "info": {
               "name": "myname"
            }
        }
    }
}

The output result is:

> require('./data.json')
{ mailOptions:
   { from: 'from me',
     service: 'gmail',
     auth: { user: 'username', pass: 'password', info: [Object] } } }

There is an answer here which suggests to use util.inspect function but for some reason it isn't working for me.

So, Two questions:

  1. I am curious to know the reason behind this
  2. How I can get the complete object serialised?

Thanks

11
  • Actually the post that you linked should work. If it don't works is a fault on your side. (Have you required util before using it?) (Have you set the depth parameter as states here: stackoverflow.com/a/21524533/1525495 ?) Commented Mar 16, 2017 at 17:25
  • @JorgeFuentesGonzález utils.inspect is ok for inspect/logging purse, but how that can be helpful to access deep objects? e.g how to access mailOptions.auth.info.name in my case? I have tried JSON.parse, but when I try to access any property on mailOptions, I get undefined. Any idea? Commented Mar 16, 2017 at 20:12
  • Don't really understand this well. When you require the data.json file, you actually get an object with all the nested elements. You just have to var data = require('./data.json'); and then console.log(data); and even console.log(data.mailOptions.auth.info);. Commented Mar 16, 2017 at 20:16
  • You are requiring it inside the node console and outputting it to the console output. If that's not "inspect/logging purposes", as you say, then I don't know what are you doing xD Where do you want the object to be serialized? In the console output? Then you should use util.inspect, yeah. Otherwise your question is malformed. Commented Mar 16, 2017 at 20:19
  • I guess that I understand what are you asking. The thing is that you should use inspect to actually see how is the object defined (you will see far from that [Object]) and then you will understand why you get undefined. Commented Mar 16, 2017 at 20:22

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.