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.
The console. log(JSON. stringify(obj)) method can be useful for logging the object to the console as string, as long as the data in the object is JSON-safe.
The simplest way is using the Console log() method to log objects as JSON in JavaScript. The console. log() method called with an object or objects as arguments will display the object or objects.
Use JSON. stringify(obj) method to convert JavaScript objects into strings and display it. Use JSON. stringify(obj, replacer, space) method to convert JavaScript objects into strings in pretty format.
json() returns a JSON object of the result (if the result was written in JSON format, if not it raises an error). Python requests are generally used to fetch the content from a particular resource URI. Whenever we make a request to a specified URI through Python, it returns a response object.
try with
console.log(JSON.stringify(assessments));
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With