jeewan
jeewan

Reputation: 1605

getJSON to console.log() to output json structure

I have the following code for getting json data:

$.getJSON( "assessments", function( assessments ) {
    console.log(assessments);
        });

I am perfectly getting all the data but the console has output as

[Object, Object, Object, Object, Object, Object, Object, Object, Object]

I want to output the values in JSON structure like this:

[
{
    "id": 1,
    "person": {
        "personId": "person1",
        "firstName": "Pactric"
    },
    "manager": {
        "managerId": "manager1"
    },
    "state": {
        "stateId": 1,
        "description": null
    },
    "comments": null
}
]

How to console.log() for this data to display exactly as above's JSON structure? I am using $.getJSON NOT $.ajax for this application.

Upvotes: 46

Views: 83168

Answers (2)

adeneo
adeneo

Reputation: 318232

Stringify the JSON with indentation like so :

$.getJSON( "assessments", function( assessments ) {
    console.log(JSON.stringify(assessments, undefined, 2))
});

JSON.stringify(value[, replacer [, space]]) where space is the indent. MDN

Upvotes: 36

Anand Jha
Anand Jha

Reputation: 10724

try with

console.log(JSON.stringify(assessments));

Upvotes: 114

Related Questions