
- 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
Flattening a JSON object in JavaScript
Suppose, we have the following JSON object that may contain nesting upto any level −
const obj = { "one": 1, "two": { "three": 3 }, "four": { "five": 5, "six": { "seven": 7 }, "eight": 8 }, "nine": 9 };
We are required to write a JavaScript function that takes in one such nested JSON object and returns a new object that contains no nesting and maps the corresponding values to the keys using the dot notation.
Therefore, in case of the object above, the output should look something like this −
const output = { 'one': 1, 'two.three': 3, 'four.five': 5, 'four.six.seven': 7, 'four.eight': 8, 'nine': 9 };
Example
The code for this will be −
const obj = { "one": 1, "two": { "three": 3 }, "four": { "five": 5, "six": { "seven": 7 }, "eight": 8 }, "nine": 9 }; const flattenJSON = (obj = {}, res = {}, extraKey = '') => { for(key in obj){ if(typeof obj[key] !== 'object'){ res[extraKey + key] = obj[key]; }else{ flattenJSON(obj[key], res, `${extraKey}${key}.`); }; }; return res; }; console.log(flattenJSON(obj));
Output
And the output in the console will be −
{ one: 1, 'two.three': 3, 'four.five': 5, 'four.six.seven': 7, 'four.eight': 8, nine: 9 }
- Related Articles
- Sorting a JSON object in JavaScript
- Flattening arrays in JavaScript
- Constructing a nested JSON object in JavaScript
- JSON group object in JavaScript
- Removing property from a JSON object in JavaScript
- Print JSON nested object in JavaScript?
- Deep Search JSON Object JavaScript
- Flattening multi-dimensional arrays in JavaScript
- Converting two arrays into a JSON object in JavaScript
- How to turn a JSON object into a JavaScript array in JavaScript ?
- How to parse JSON object in JavaScript?
- How to convert JSON text to JavaScript JSON object?
- Finding the smallest value in a JSON object in JavaScript
- How to deserialize a JSON into Javascript object?
- Flattening a deeply nested array of literals in JavaScript

Advertisements