How to Convert nested json to array of object in Javascript?

chans

I've a deep nested object like this:

{
  'name_1': 'val',
  'name_2': 'val',
  'name_3': {
    'name_4': {
      'name_5': {
        'name_6': 'val',
        'name_7': 'val',
        'name_8': {
          'name_9': 'val'
        }
      }
    }
  }
}

Here key can be anything instead of name_1, name_2, name_3...etc(names key as name_1, name_2.. just for quick understanding)

I wanted to convert the above json to array of objects in the below mentioned format

[
  { "id": "1", "name": 'name_1: val', "parent_id": "0"},
  { "id": "2", "name": 'name_2: val', "parent_id": "0"},
  { "id": "3", "name": 'name_3', "parent_id": "0"},
  { "id": "4", "name": 'name_4', "parent_id": "3"},
  { "id": "5", "name": 'name_5', "parent_id": "4"},
  { "id": "6", "name": 'name_6: val', "parent_id": "5"},
  { "id": "7", "name": 'name_7: val', "parent_id": "5"},
  { "id": "8", "name": 'name_8', "parent_id": "5"},
  { "id": "9", "name": 'name_9: val', "parent_id": "8"},
]

Any help would be really apprectiated!!

Nina Scholz

You could take an iterative and recursive approach. (The id is stored in an object to keep the same object reference for it and this allows to use it in all nested recursive calls.)

function flat(object, id = { id: 0 }, parent_id = 0) {
    return Object.entries(object).reduce((r, [k, v]) => {
        var name = k + (typeof v === 'string' ? ': ' + v : '');
        r.push({ id: id.id++, name, parent_id });
        if (v && typeof v === 'object') r.push(...flat(v, id, id.id));
        return r;        
    }, []);
}

var data = { name_1: 'val', name_2: 'val', name_3: { name_4: { name_5: { name_6: 'val', name_7: 'val', name_8: { name_9: 'val' } } } } },
    result = flat(data);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert JSON object to JavaScript array?

How to convert Array into json object?

Convert Array object into Json in Javascript

How to convert mongodb response array from javascript object to JSON string

How to convert nested set into nested array in javascript?

How to convert json to array object in javascript?

Javascript - Convert Nested Object to Array of items

Convert Array of Objects to Nested Object in Javascript

how to convert plain array into nested object which is nested into parent object

How to convert array to a json object?

convert nested json array to javascript array

How to convert JSON Object into Javascript array

convert json object to javascript array

how to convert this JavaScript object that contains an array to JSON?

How to Convert a nested array to an object

how to convert objects in array which is already in a nested object in javaScript

How to convert an array of JSON into an array of javascript object?

Convert JSON Array to a JSON Object using Javascript

How to convert an array of nested objects to an object with key, value pairs in JavaScript

is there any way to convert nested object to array in javascript

convert nested json object to formdata javascript for Multer

how to Convert JavaScript array (without object name) into json (with object name)?

how convert my nested object to array in javascript

How to convert nested object to array of object in javascript?

How do you convert an array to a nested object in javascript?

How to convert array with nested object to object?

Convert array to nested object structure using Javascript

React - How to convert a nested object into a array

How to convert JSON array to OBJECT array javascript?