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

Community
  • 1
  • 1
Nexus23
  • 6,195
  • 9
  • 50
  • 67
  • 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: http://stackoverflow.com/a/21524533/1525495 ?) – Jorge Fuentes González Mar 16 '17 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? – Nexus23 Mar 16 '17 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);`. – Jorge Fuentes González Mar 16 '17 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. – Jorge Fuentes González Mar 16 '17 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. – Jorge Fuentes González Mar 16 '17 at 20:22
  • Have u actually tried this or you are just guessing that it will work? Can you paste the code which works for you to access deep property? I am getting undefined for properties which display as Object – Nexus23 Mar 17 '17 at 06:49
  • Again, properties display as Object because you are not inspecting it. The console breaks them, but that don't means that they are not there! Do this: `require("util").inspect(require('./data.json'), { depth: null })`. This way you will see the entire object and will know why those elements are undefined. I hope this time you catch it xD – Jorge Fuentes González Mar 17 '17 at 08:12
  • With your code, this is what I get in console: { mailOptions: { from: 'from me', service: 'gmail', auth: { user: 'username', pass: 'password', info: { name: 'myname' } } } } But when i try to access first property e.g mailOptions, I get undefined. Here is the code: require("util").inspect(require('./json.json'), { depth: null }).mailOptions – Nexus23 Mar 17 '17 at 09:25
  • No, inspect returns a string only for reading, not for interacting. If you want to access one of the elements programmatically, return to one of my previous messages, this one exactly: "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);`". The inspect thingy is for reading by eye what's inside info, instead of getting the weird "[Object]" thing, but is an string. – Jorge Fuentes González Mar 17 '17 at 13:34
  • Now that you see what are the contents of the element, you can read it with what I said above. The inspect thing if for clarify your "Why I get an [Object] instead of the actual contents?" (the second question). Actually, the object does contains the info. The console showing otherwise is just to not clog the output, but the console showing [Object] don't means that is not an object with properties inside the script. – Jorge Fuentes González Mar 17 '17 at 13:34
  • Thanks @JorgeFuentesGonzález you are right. My mistake. Simple require json file and access the property works as normal. The reason why it led me to a point where I wanted to resolve deep object access through util.inspec was that in my code I was able to require('./data.json') but the deep object was coming in as undefined. It might be I was missing something. But apologies for consusing. Closing this for now. – Nexus23 Mar 17 '17 at 15:16

0 Answers0