tchan
tchan

Reputation: 781

How to check for the existence of a null object in a nested object in JavaScript?

Say for example if I have a 3 level deep javascript object

{CONN_INFO: {CFGSwitch: {412: {}}}}

How can I write a function that determines if it is nested? And secondly, how can I then convert the empty object {} to a string such as "{}"

Upvotes: 0

Views: 529

Answers (2)

Yash Soni
Yash Soni

Reputation: 456

How can I write a function that determines if it is nested?

For all keys of the current object, check its type, if JSON then nesting is present.

And secondly, how can I then convert the empty object {} to a string such as "{}"?

If you encounter a nested JSON object, check for its key length using Object.keys(currentJSONObj).length, if 0 then this is an empty JSON. So re-assign "{}" to the key which had empty JSON object at first place.

Upvotes: 1

Gopal
Gopal

Reputation: 745

Iterate and check Object.keys(current_obj).length

Upvotes: 0

Related Questions