5

I want to map one json data to a new javascript object like following. Here the json data is dynamic and can have more files with more users. The group information is new and it depends on parent-child information. Can anyone please help me out? Thank you for your time.

Before:

{
  "userinfo": {
    "/home/user/main/sub/info/1stfile.txt": {
      "John": "something",
      "Mike": "something",
      "Merry": "something",
      "Susan": "something"
    },
    "/home/user/main/info/2ndfile.txt": {
      "Mulan": "something",
      "James": "something"
    },
    "/home/user/main/info/3rdfile.txt": {
      "Nancy": "something"
    },
    "/home/user/main/4thfile.txt": {
      "Kamal": "something",
      "Xian": "something",
      "Mila": "something"
    }
  }
}

After:

{
  "name": "main",
  "children": [
    {
      "name": "1stfile",
      "children": [
        {
          "name": "John",
          "group": "1"
        },
        {
          "name": "Mike",
          "group": "1"
        },
        {
          "name": "Merry",
          "group": "1"
        },
        {
          "name": "Susan",
          "group": "1"
        }
      ],
      "group": 1
    },
    {
      "name": "2ndfile",
      "children": [
        {
          "name": "Mulan",
          "group": 2
        },
        {
          "name": "James",
          "group": 2
        }
      ],
      "group": 2
    },
    {
      "name": "3rdfile",
      "children": [
        {
          "name": "Nancy",
          "group": 3
        }
      ],
      "group": 3
    },
    {
      "name": "4thfile",
      "children": [
        {
          "name": "Kamal",
          "group": 4
        },
        {
          "name": "Xian",
          "group": 4
        },
        {
          "name": "Mila",
          "group": 4
        }
      ],
      "group": 4
    }
  ],
  "group": 0
}

I was trying to build one block of parent-child by using following code

var jsonData = json["userinfo"];
var keys = Object.keys(jsonData);
console.log(keys);
let data = {};
for (var j = 0; j < keys.length; j++) {
  let g = 1;
  data[j] = { name: keys[j], group: g++ };
}
console.log(data);

Which is giving following output

{
  0: {
    "name": "/home/user/main/sub/info/1stfile.txt",
    "group": 1
  },
  1: {
    "name": "/home/user/main/info/2ndfile.txt",
    "group": 1
  },
  2: {
    "name": "/home/user/main/info/3rdfile.txt",
    "group": 1
  },
  3: {
    "name": "/home/user/main/4thfile.txt",
    "group": 1
  }
}

The value is assigning properly but is creating extra keys (0,1,2,3)!

4
  • 1
    Can you show us what have you attempted so far ? Commented Nov 15, 2019 at 15:42
  • I have included the code snippet. Commented Nov 15, 2019 at 15:58
  • Can you provide a valid before and after ? A valid example of before and after Commented Nov 15, 2019 at 16:08
  • This is a valid example for my task. I've just replaced the user information with "something". Commented Nov 15, 2019 at 16:10

1 Answer 1

2

Assuming you need something like this.

You can utilize Array.map() and Object.keys() function for your operation.

<script>
  const beforeJSON = `{
    "userinfo": {
      "/home/user/main/sub/info/1stfile.txt": {
        "John": "something",
        "Mike": "something",
        "Merry": "something",
        "Susan": "something"
      },
      "/home/user/main/info/2ndfile.txt": {
        "Mulan": "something",
        "James": "something"
      },
      "/home/user/main/info/3rdfile.txt": {
        "Nancy": "something"
      },
      "/home/user/main/4thfile.txt": {
        "Kamal": "something",
        "Xian": "something",
        "Mila": "something"
      }
    }
  }`
  const before = JSON.parse(beforeJSON);
  const filenames = Object.keys(before.userinfo);

  const after = {
    name: 'main',
    children: [],
    group: 0,
  }

  const children = filenames.map((filename, idx) => {
    const innerChildren = Object.keys(before.userinfo[filename]).map((n) => ({
      name: n,
      group: idx + 1,
    }))
    return ({
      name: filename,
      children: innerChildren,
      group: idx + 1,
    });
  })

  after.children = children;

  console.log(after);
</script>

Please format your code next time before posting another question.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much and I'll properly format my questions in future.

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.