-2

I have an array on objects like this,

[
  {
    "link": "link1",
    "model": "model1",
    "role": "role1",
    "access": "true"
  },
  {
    "link": "link1",
    "model": "model1",
    "role": "role2",
    "access": "true"
  },
  {
    "link": "link1",
    "model": "model1",
    "role": "role3",
    "access": "true"
  },
  {
    "link": "link1",
    "model": "model1",
    "role": "role4",
    "access": "true"
  },
  {
    "link": "link1",
    "model": "model2",
    "role": "role1",
    "access": "false"
  },
  {
    "link": "link1",
    "model": "model2",
    "role": "role2",
    "access": "false"
  },
  {
    "link": "link1",
    "model": "model2",
    "role": "role3",
    "access": "false"
  },
  {
    "link": "link1",
    "model": "model2",
    "role": "role4",
    "access": "false"
  },
  {
    "link": "link2",
    "model": "model1",
    "role": "role1",
    "access": "false"
  },
  {
    "link": "link2",
    "model": "model1",
    "role": "role2",
    "access": "true"
  },
  {
    "link": "link2",
    "model": "model1",
    "role": "role3",
    "access": "false"
  },
  {
    "link": "link2",
    "model": "model1",
    "role": "role4",
    "access": "true"
  },
  {
    "link": "link2",
    "model": "model2",
    "role": "role1",
    "access": "false"
  },
  {
    "link": "link2",
    "model": "model2",
    "role": "role2",
    "access": "true"
  },
  {
    "link": "link2",
    "model": "model2",
    "role": "role3",
    "access": "false"
  },
  {
    "link": "link2",
    "model": "model2",
    "role": "role4",
    "access": "true"
  }
]

With respect to this question, the input was a CSV sample with very less number of rows. In real I have a large CSV file and I found a way to import it and convert to JSON using d3.js, but the problem is I'm unable to convert it to the desired format. I tried using the data.forEach and the output was really weird and I couldn't understand why I get model2 alone in both the links.

Code:

d3.csv('data.csv', function(data) {

            var newData = {};

            data.forEach(function(e, i) {
                newData[e.link] = {};
                newData[e.link][e.model] = {};
            })

            d3.select('main').append('pre')
                .text(JSON.stringify(newData, null, '  '));
        });

Output:

{
  "link1": {
    "model2": {}
  },
  "link2": {
    "model2": {}
  }
}




Desired Output:

"link1": {
    "model1": {
        "role1": true,
        "role2": true,
        "role3": true,
        "role4": true,
    },
    "model2": {
        "role1": false,
        "role2": false,
        "role3": false,
        "role4": false,
    }
},
"link2": {
    "model1": {
        "role1": false,
        "role2": true,
        "role3": false,
        "role4": true,
    },
    "model2": {
        "role1": false,
        "role2": true,
        "role3": false,
        "role4": true,
    }
}

Any help will be much appreciated. Thanks in advance.

4
  • Which is the question? Commented Sep 21, 2017 at 4:31
  • This is the question, I wanted to convert the JSON in the top, like the one in the bottom. Commented Sep 21, 2017 at 4:40
  • Always this will have 4 roles? role1, role2, role3, role4 ? or is that dynamic? Commented Sep 21, 2017 at 4:47
  • No, it is not same always. Some will have 2 and some with 3 and 4 Commented Sep 21, 2017 at 4:53

1 Answer 1

1

In your forEach, you're not assigning the role and access pair on each iteration, and you're always emptying each section.

Change your forEach with this...

data.forEach(function(e, i) {

    // Check if this "link" already exists exists, if not, create it.
    if (!newData[e.link])
        newData[e.link] = {};

    // Check if this "model" already exists inside of this "link", if not, create it.
    if (!newData[e.link][e.model])
        newData[e.link][e.model] = {};

    newData[e.link][e.model][e.role] = e.access;
});

I hope it helps

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

Comments

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.