JavaEvgen JavaEvgen - 4 months ago 9
Javascript Question

Getting object Object instead of value "reading" through json object

I use d3 library to read json from file. My json is a loong line of keys and values so I use code below to make it more "readable"

d3.json("/try.json", function(error, input) {
for(var key in input){
console.log(key+ ' : '+input[key]);
}
});


Output I get is:

type : user_interaction
user_interaction : [object Object]
time : [object Object]
event_id : 771ae5e3-c9d0-4950-b349-55ceeb35f7ee
device : [object Object]
source : product-a
format : base-v2-client
tenant_id : 46bb7e3c2f0d01a3aa4a3b21a3b93fd886cdba88
application : [object Object]
consent : [object Object]
network : [object Object]
timestamp : 1433661896414
sender_info : [object Object]
system : [object Object]


So my problem is that if a value of a key is an object I get "object Object" instead of a value. But if my console.log would look like this:

console.log(input[key]);


All the values would be displayed

{ type: 'view', view_id: 'msclient:view.main' }
{ create_timestamp: 1433661911699,
send_timestamp: 1433661911703 }


Above is a part of output for user_interaction and time (both had object Object instead of value in the first example)

I could of use

console.log(key+ ' : ');
console.log(input[key]);


but then the output looks ugly:

type :
user_interaction
user_interaction :
{ type: 'view', view_id: 'msclient:view.main' }
time :
{ create_timestamp: 1433661911699,
send_timestamp: 1433661911703 }


So the question is, is it possible to make the output from the first example have values instead of object Object things?

I apologize for a long text,
Thanx in advance!

Answer

You were converting the object to a string by concatenating it to a string value. Try this instead.

  d3.json("/try.json", function(error, input) {
        for(var key in input){
            console.log(key, ':', input[key]); // do not convert object to string
        }
    });