How to print a circular structure in a JSON-like format?

0 votes

I want to convert to JSON and send. However it has circular structure. I want to toss whatever circular references exist and send whatever can be stringified. How do I do that?

Thanks.

var obj = {
  a: "foo",
  b: obj
}

I want to stringify obj into:

{"a":"foo"}
Apr 24 in Java-Script by kartik
• 11,890 points
28 views

1 answer to this question.

0 votes

Hello,

You can use util.inspect(object) in node.js. It automatically replaces circular links with "[Circular]".

Albeit being built-in (no installation is required), you must import it

import * as util from 'util' // has no default export
import { inspect } from 'util' // or directly
// or 
var util = require('util')

To use it, simply call

console.log(util.inspect(myObject))

Also be aware that you can pass options object to inspect (see link above)

inspect(myObject[, options: {showHidden, depth, colors, showProxy, ...moreOptions}])

Hope this works!!

Thank You!!

answered Apr 24 by Niroj
• 23,950 points

Related Questions In Java-Script

0 votes
1 answer

How to access PHP session variables from jQuery function in a .js file?

Hello, You can produce the javascript file via ...READ MORE

answered Apr 29 in Java-Script by Niroj
• 23,950 points
210 views
0 votes
1 answer

How to Get values from a specific user - queryset in Django?

Hello @kartik, You can get the list of Users ...READ MORE

answered May 28 in Java-Script by Niroj
• 23,950 points
18 views
0 votes
1 answer

How to clone a JavaScript object?

Hello, You can clone an object and remove ...READ MORE

answered Apr 2 in Java-Script by Niroj
• 23,950 points
17 views
0 votes
1 answer

How to run multiple npm scripts in parallel?

You should use npm-run-all (or concurrently, parallelshell), because it has more ...READ MORE

answered Apr 24 in Java-Script by Niroj
• 23,950 points
73 views
0 votes
1 answer

Why it is necessary to refresh CSRF token per form request?

Hello, Generating a new CSRF token for each ...READ MORE

answered Mar 19 in Laravel by Niroj
• 23,950 points
199 views
0 votes
1 answer

What is meant by passing the variable by value and reference in PHP?

Hello, When the variable is passed as value ...READ MORE

answered Mar 27 in PHP by Niroj
• 23,950 points
41 views
0 votes
1 answer

Connection with MySQL server using PHP. How can we do that?

Hey @kartik, You have to provide MySQL hostname, ...READ MORE

answered Mar 27 in PHP by Niroj
• 23,950 points
44 views
0 votes
1 answer

How to retrieve or obtain data from the MySQL database using PHP?

Hello kartik,  Actually there are many functions that  ...READ MORE

answered Mar 27 in PHP by Niroj
• 23,950 points
36 views
0 votes
1 answer

How do I turn a string to a json in Node.js?

Hello Kartik, Use the JSON function  JSON.parse(theString) ...READ MORE

answered Apr 24 in Java-Script by Niroj
• 23,950 points
23 views
+1 vote
1 answer

How can we send message multiple time to a specific person or group in whatsapp using loop?

Hii @kartik,  This is simple task to send single ...READ MORE

answered Feb 28 in Java-Script by Niroj
• 23,950 points
250 views