DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Printing JSON data on Node.js

Printing JSON data on Node.js

Axel Rauschmayer user avatar by
Axel Rauschmayer
·
Oct. 24, 11 · Web Dev Zone · Interview
Like (0)
Save
Tweet
8.92K Views

Join the DZone community and get the full member experience.

Join For Free
Problem: If a data structure is too deeply nested, Node.js stops showing the details. This post presents two solutions.

Display a value as JSON data

Node.js stops displaying a data structure after two levels of nesting:
    > [[[ "test" ]]]
    [ [ [ 'test' ] ] ]

    > [[[[ "test" ]]]]
    [ [ [ [Object] ] ] ]
In this case [Object] stands for [ "test" ].

Option 1: a single line of JSON.

    > console.log("%j", [[[[ "test" ]]]]);
    [[[["test"]]]]
Option 2: Use JSON.stringify() [1] to print an indented tree.
    var tree = function(x) { console.log(JSON.stringify(x, null, 4)) };
    > tree([[[[ "test" ]]]]);
    [
        [
            [
                [
                    "test"
                ]
            ]
        ]
    ]
Warning: Anything non-JSONy will not be printed.
    > console.log("%j", { foo: function() {}, bar: "abc" })
    {"bar":"abc"}

Use util.inspect()

You can also use util.inspect(), but you need to specify that the nesting depth should be unlimited (the default is 2). It is also better not to show non-enumerable properties to avoid recursing into the properties of a function.

Option 1: Use util.inspect() directly.

    var util = require("util");
    > function ins(value) { console.log(util.inspect(value, false, null)); }

    > ins([[[[ "test" ]]]])
    [ [ [ [ 'test' ] ] ] ]

    > ins({ foo: function() {}, bar: "abc" });
    { foo: [Function], bar: 'abc' }

Option 2: Use console.dir(), which internally calls util.inspect(). Drawback: Stops after a nesting depth of 2.

    > console.dir([[[[ "test" ]]]])
    [ [ [ [Object] ] ] ]

    > console.dir({ foo: function() {}, bar: "abc" });
    { foo: [Function], bar: 'abc' }

Further reading

  1. JavaScript’s JSON API
JSON Data (computing) Node.js Printing

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Open Source Security Risks
  • Top Soft Skills to Identify a Great Software Engineer
  • After COVID, Developers Really Are the New Kingmakers
  • Synchronization Methods for Many-To-Many Associations

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo