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.

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? Commented Jul 12, 2018 at 16:43
  • Possible duplicate of Grouping JSON by values Basically var groupedByName = groupBy(json.children, 'name') Commented Jul 12, 2018 at 17:07

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.