serkan
serkan

Reputation: 7141

flattening nested json object

[
  {
    "children": [
      {
        "children": [
          {
            "dateAdded": 1493033302670,
            "id": "1534",
            "index": 0,
            "parentId": "1",
            "title": "data1",
            "url": "data2"
          },
          {
            "children": [
              {
                "dateAdded": 1489571506844,
                "id": "1451",
                "index": 0,
                "parentId": "1401",
                "title": "data3",
                "url": "data4"
              }
            ],
            "dateAdded": 1490363326576,
            "dateGroupModified": 1490363326576,
            "id": "1401",
            "index": 1,
            "parentId": "1",
            "title": "daily"
          },
          {
            "children": [
              {
                "dateAdded": 1481787664555,
                "id": "1429",
                "index": 0,
                "parentId": "1407",
                "title": "data56",
                "url": "data"
              },
              {
                "dateAdded": 1483365608504,
                "id": "1430",
                "index": 1,
                "parentId": "1407",
                "title": "data34",
                "url": "data55"
              }
            ]
          }
        ]
      }
    ]
  }
]

This is a representation of Chrome bookmarks data.

If the object has url property it means that is a bookmark. If it does not have url property it is a folder.

It is a tree structure.

I would like to create flatten object with additional property named type. Like:

[
{
    "dateAdded": 1489571506844,
    "id": "1451",
    "index": 0,
    "parentId": "1401",
    "title": "title",
    "url": "some url",
    "type": "bookmark"

},
{
    "dateAdded": 1489571506844,
    "id": "1451",
    "index": 0,
    "parentId": "1402",
    "title": "title2",
    "url": "some url2"
    "type": "folder"
}
] 

Thanks in advance.

Upvotes: 0

Views: 5188

Answers (3)

fwBasic
fwBasic

Reputation: 162

The example shows how to do it

data = [
  {
    "children": [
      {
        "children": [
          {
            "dateAdded": 1493033302670,
            "id": "1534",
            "index": 0,
            "parentId": "1",
            "title": "data1",
            "url": "data2"
          },
          {
            "children": [
              {
                "dateAdded": 1489571506844,
                "id": "1451",
                "index": 0,
                "parentId": "1401",
                "title": "data3",
                "url": "data4"
              }
            ],
            "dateAdded": 1490363326576,
            "dateGroupModified": 1490363326576,
            "id": "1401",
            "index": 1,
            "parentId": "1",
            "title": "daily"
          },
          {
            "children": [
              {
                "dateAdded": 1481787664555,
                "id": "1429",
                "index": 0,
                "parentId": "1407",
                "title": "data56",
                "url": "data"
              },
              {
                "dateAdded": 1483365608504,
                "id": "1430",
                "index": 1,
                "parentId": "1407",
                "title": "data34",
                "url": "data55"
              }
            ]
          }
        ]
      }
    ]
  }
];
data2 = [];
function search(data) {
 for (n in data) {
  if (typeof data[n] == 'object') {
   if (data[n].id != undefined) {
    if (data[n].url != undefined) {
     data[n].type="folder";
    } else {
     data[n].type="bookmark";
    }
    data2.push(data[n]);
   }
   search(data[n]);
  }
 }
}
search(data);
console.log(data2);

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386570

You could use an iterative and recursive approach for getting flat data.

function flatten(array) {
    var result = [];
    array.forEach(function iter(o) {
        var temp = {},
            keys = Object.keys(o);

        if (keys.length > 1) {
            keys.forEach(function (k) {
                if (k !== 'children') {
                    temp[k] = o[k];
                }
            });
            temp.type = 'url' in o ? 'bookmark' : 'folder';
            result.push(temp);
        }
        Array.isArray(o.children) && o.children.forEach(iter);
    });
    return result;
}

var data = [{ children: [{ children: [{ dateAdded: 1493033302670, id: "1534", index: 0, parentId: "1", title: "data1", url: "data2" }, { children: [{ dateAdded: 1489571506844, id: "1451", index: 0, parentId: "1401", title: "data3", url: "data4" }], dateAdded: 1490363326576, dateGroupModified: 1490363326576, id: "1401", index: 1, parentId: "1", title: "daily" }, { children: [{ dateAdded: 1481787664555, id: "1429", index: 0, parentId: "1407", title: "data56", url: "data" }, { dateAdded: 1483365608504, id: "1430", index: 1, parentId: "1407", title: "data34", url: "data55" }] }] }] }];

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

Upvotes: 2

Toby Mellor
Toby Mellor

Reputation: 8205

I've made a function that iterates through an array containing objects. If a given object has a property called children, the function calls itself. If it doesn't, then it gets pushed to a new array flattenedBookmarks.

The Solution

var flattenedBookmarks = [];

flattenBookmarks(bookmarks);

function flattenBookmarks(bookmarks) {
    for (var i = 0; i < bookmarks.length; i++) {
        var potentialBookmark = bookmarks[i];

        if (potentialBookmark.hasOwnProperty("url")) {
            potentialBookmark.type = "bookmark";
        } else {
            potentialBookmark.type = "folder";
        }

        if (potentialBookmark.hasOwnProperty("children")) {
            flattenBookmarks(potentialBookmark.children);

            if (potentialBookmark.hasOwnProperty("dateGroupModified")) {
                flattenedBookmarks.push(potentialBookmark);
            }
        } else {
            flattenedBookmarks.push(potentialBookmark);
        }
    }
}

You should probably be returning the flattened array from the function instead of storing it in a new global array flattenedBookmarks, but at least this will get you started.

https://jsfiddle.net/s9ur35re/

Upvotes: 2

Related Questions