
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Convert JSON to another JSON format with recursion JavaScript
Suppose, we have the following JSON object −
const obj = { "context": { "device": { "localeCountryCode": "AX", "datetime": "3047-09-29T07:09:52.498Z" }, "currentLocation": { "country": "KM", "lon": -78789486, } } };
We are required to write a JavaScript recursive function that initially takes in one such array.The function should split the above object into a "label" - "children" format.
Therefore, the output for the above object should look like −
const output = { "label": "context", "children": [ { "label": "device", "children": [ { "label": "localeCountryCode" }, { "label": "datetime" } ] }, { "label": "currentLocation", "children": [ { "label": "country" }, { "label": "lon" } ] } ] }
The code for this will be −
Example
const obj = { "context": { "device": { "localeCountryCode": "AX", "datetime": "3047-09-29T07:09:52.498Z" }, "currentLocation": { "country": "KM", "lon": -78789486, } } }; const transformObject = (obj = {}) => { if (obj && typeof obj === 'object') { return Object.keys(obj).map((el) => { let children = transformObject(obj[el]); return children ? { label: el, children: children } : { label: el }; }); }; }; console.log(JSON.stringify(transformObject(obj), undefined, 4));
Output
And the output in the console will be −
[ { "label": "context", "children": [ { "label": "device", "children": [ { "label": "localeCountryCode" }, { "label": "datetime" } ] }, { "label": "currentLocation", "children": [ { "label": "country" }, { "label": "lon" } ] } ] } ]
- Related Articles
- How to convert MySQL DATETIME value to JSON format in JavaScript?
- How to convert JSON text to JavaScript JSON object?
- How to convert Python date in JSON format?
- Convert JSON array into normal json in JavaScript
- How to format JSON string in JavaScript?
- JavaScript Convert an array to JSON
- Transform tree from DB format to JSON format in JavaScript
- Strip quotes with JavaScript to convert into JSON object?
- How to convert JSON string to array of JSON objects using JavaScript?
- How to convert JSON object to Hashtable format using PowerShell?
- Convert a JSON object to XML format in Java?\n
- How to convert the hashtable to the JSON format using PowerShell?
- Update a MySQL column with JSON format?
- Explain JSON format in PowerShell.
- How to create JSON format with group-concat in MySQL?

Advertisements