0

I'm trying to change the structure of a json by removing duplicate keys. Otherwise, to put the children of a same name inside only one name node.

Current JSON:

{
    "name": "flare",
    "children": [
        {
            "name": "analytics",
            "children": [
                {
                    "name": "cluster",
                    "children": [
                        {
                            "name": "AgglomerativeCluster",
                            "size": [
                            "3938"
                            ]
                        }
                    ]
                }
            ]
        },
        {
            "name": "analytics",
            "children": [
                {
                    "name": "cluster",
                    "children": [
                        {
                            "name": "CommunityStructure",
                            "size": [
                            "3812"
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

Desired output:

{
    "name": "flare",
    "children": [
        {
            "name": "analytics",
            "children": [
                {
                    "name": "cluster",
                    "children": [
                        {
                            "name": "AgglomerativeCluster",
                            "size": 3938
                        },
                        {
                            "name": "CommunityStructure",
                            "size": 3812
                        }
                    ]
                }
            ]
        }
    ]
};

Thanks for your help.

derloopkat
  • 6,232
  • 16
  • 38
  • 45
j2020
  • 3
  • 2
  • 1
    What specifically do you need help with? Do you know how the basics about iterating over arrays and objects and accessing them? Do you need help with developing on algorithm to perform this operation? Have you tried anything? – Felix Kling Jul 12 '18 at 16:43
  • Possible duplicate of [Grouping JSON by values](https://stackoverflow.com/questions/38575721/grouping-json-by-values) Basically ``var groupedByName = groupBy(json.children, 'name')`` – derloopkat Jul 12 '18 at 17:07

1 Answers1

2

Typically, StackOverflow isn't the place to have people write code for you, and your question should be more specific as to with what part of your algorithm you are having trouble. However, this looked fun, so I did it.

I solved this by first converting it to an object whose properties are the names and values are the children/size. This insured that each named instance was grouped with other named instances.

var mutate = function(desired, current) {
  for (var x = 0; x < current.length; x++) {
    if (Object.hasOwnProperty.call(current[x], 'size')) {
      desired[current[x].name] = parseInt(current[x].size[0], 10);
    }
    else {
      if (!Object.hasOwnProperty.call(desired, current[x].name)) {
        desired[current[x].name] = Object.create(null);
      }
      mutate(desired[current[x].name], current[x].children);
    }
  }
  return desired;
};

I then converted that back to your original desired format by iterating over the Object.entries (key/value pairs).

var mutate2 = function(current) {
  var desired = [];
  var entries = Object.entries(current);
  for (var x = 0; x < entries.length; x++) {
    var o = Object.create(null);
    o.name = entries[x][0];
    if (typeof entries[x][1] === 'number') {
      o.size = entries[x][1];
    }
    else {
      o.children = mutate2(entries[x][1]);
    }
    desired.push(o);
  }
  return desired;
};

You get your result by using this hideous beast:

var desiredJson = mutate2(mutate(Object.create(null), [ currentJson ]));
console.log(desiredJson);
Charles Stover
  • 1,132
  • 6
  • 13
  • Thanks for your answer Charles. I was trying several things. But as I've never used javascript before, it was bit difficult for me. Your solution solved my problem, thanks again. – j2020 Jul 12 '18 at 17:31