0

I'm just trying to read json objects from a json array. But I'm getting [Object] in place of nested Json objects while trying to read the whole array.

My JSON looks like this:

var json = {
  "root_id": [
    {
      "child-id1": {
        "name": "name1",
        "created_by": null,
        "created_at": "2018-05-30T19:34:38.657Z",
        "configs": {
          "generic": {
            "size": 1000,
            "timeout": 60,
            "field1_key": "field1_value"
          },
          "specific": {
            "key1": "xxx-xxx",
            "field1_key": "field1_value"
          }
        }
      }
    },
    {
      "child-id2": {
        "name": "name2",
        "created_by": null,
        "created_at": "2018-05-30T19:34:38.657Z",
        "configs": {
          "generic": {
            "size": 10,
            "timeout": 60,
            "field1_key": "field1_value"
          },
          "specific": {
            "key1": "xxx-xxx",
            "field1_key": "field1_value"
          }
        }
      }
    }
  ]
}

I want my function return the Json array of "root_id" object. So, I just tried this simple code to read the array:

var val = json['root_id'];
console.log(val);

But it returns this:

[ { 'child-id1': 
     { name: 'name1',
       created_by: null,
       created_at: '2018-05-30T19:34:38.657Z',
       configs: [Object] } },
  { 'child-id2': 
     { name: 'name2',
       created_by: null,
       created_at: '2018-05-30T19:34:38.657Z',
       configs: [Object] } } ]

How do i make sure the nested objects return as it is instead of just [object]?

saz
  • 955
  • 5
  • 15
  • 26
  • 2
    Is this what it returns or what `console.log` displays? If you want to show everything in the console, JSON.stringify it. – Luca Kiebel Jul 09 '18 at 18:30
  • `console.log` displays `[Object]` when an object has three levels down. You can use `console.log(JSON.stringify(yourObject))` and this may show you the entire JSON. – Andrés Encarnación Jul 09 '18 at 18:41
  • 1
    `console.log(util.inspect(val, { compact: true, depth: 15, breakLength: 80 }));` – baao Jul 09 '18 at 19:02

2 Answers2

4

If you are using this console.log in the server you should use util.inspect(), https://nodejs.org/api/util.html.

Check this question: How can I get the full object in Node.js's console.log(), rather than '[Object]'?

If you use in the browser it should show everything.

Guzz
  • 66
  • 2
0

Try

var json = {
  "root_id": [
    {
      "child-id1": {
        "name": "name1",
        "created_by": null,
        "created_at": "2018-05-30T19:34:38.657Z",
        "configs": {
          "generic": {
            "size": 1000,
            "timeout": 60,
            "field1_key": "field1_value"
          },
          "specific": {
            "key1": "xxx-xxx",
            "field1_key": "field1_value"
          }
        }
      }
    },
    {
      "child-id2": {
        "name": "name2",
        "created_by": null,
        "created_at": "2018-05-30T19:34:38.657Z",
        "configs": {
          "generic": {
            "size": 10,
            "timeout": 60,
            "field1_key": "field1_value"
          },
          "specific": {
            "key1": "xxx-xxx",
            "field1_key": "field1_value"
          }
        }
      }
    }
  ]
}
 
var count = Object.keys(json['root_id']).length;
for (var i = 0; i < count; i++) {
    console.log(json['root_id'][i]);
}