Get a JSON data structure from a JSON object

In this article, we will show you how to get a JSON data structure from a JSON object. This is very small article where we will use the different type of the JSON object to get the all possible JSON data structure using JavaScript.

For the demo purpose, we will use the following JSON objects to get the JSON data structure.

JSON object 1

JSON object 2

Function to get a JSON data structure

Let’s write a function to get a JSON data structure based on the JSON object. In this function, we will pass two parameters, the obj that used to pass the JSON object and str that used to pass the prefix for the data structure.

To read the nested structure, we will write a recursive function and if the type of the attribute’s value does not match the object then we will push the structure into the list.

Output

Finally, we will call the function to get the JSON data structure list.

Output: JSON object 1

Output: JSON object 2

That’s it for today.
Thank you for reading. Happy Coding..!!

Demo & Source Code

StackBlitz Project
If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *