
- 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 array into normal json in JavaScript
Suppose, we have a JSON array with key/value pair objects like this −
const arr = [{ "key": "name", "value": "john" }, { "key": "number", "value": "1234" }, { "key": "price", "value": [{ "item": [{ "item": [{ "key": "quantity", "value": "20" }, { "key": "price", "value": "200" }] }] }] }];
We are required to write a JavaScript function that takes in one such array.
The function should prepare a new array where data is simply listed against key value instead of this complex structure.
Therefore, for the above array, the output should look like this −
const output = { "name": "john", "number": "1234", "price": { "quantity": "20", "price": "200" } };
Example
The code for this will be −
const arr = [{ "key": "name", "value": "john" }, { "key": "number", "value": "1234" }, { "key": "price", "value": [{ "item": [{ "item": [{ "key": "quantity", "value": "20" }, { "key": "price", "value": "200" }] }] }] }]; const simplify = (arr = []) => { const res = {}; const recursiveEmbed = function(el){ if ('item' in el) { el.item.forEach(recursiveEmbed, this); return; }; if (Array.isArray(el.value)) { this[el.key] = {}; el.value.forEach(recursiveEmbed, this[el.key]); return; }; this[el.key] = el.value; }; arr.forEach(recursiveEmbed, res); return res; }; console.log(simplify(arr));
Output
And the output in the console will be −
{ name: 'john', number: '1234', price: { quantity: '20', price: '200' } }
- Related Articles
- How to convert JSON Array to normal Java Array?
- JavaScript Convert an array to JSON
- How to convert JSON string to array of JSON objects using JavaScript?
- Strip quotes with JavaScript to convert into JSON object?
- Convert JSON to another JSON format with recursion JavaScript
- How to convert JSON text to JavaScript JSON object?
- How to convert a JSON string into a JavaScript object?
- How to convert JSON results into a date using JavaScript?
- Regroup JSON array in JavaScript
- How can I convert a bytes array into JSON format in Python?
- How to turn a JSON object into a JavaScript array in JavaScript ?
- How to convert an array to JSON Array using JSON-lib API in Java?\n
- How to convert a JSON array to array using JSON-lib API in Java?\n
- Formatting dynamic json array JavaScript
- How to convert JSON string into Lua table?

Advertisements