0

I want to convert data table to Json. I use node.js I have Data Table as :

 -------------------------------------
 |   Month    |    Name    |   Sum   |
 -------------------------------------
 |   January  |    John    |   25    |
 |   February |    Jane    |   30    |
 |   February |    John    |   35    |
 |   February |    Alex    |   20    |
 |   March    |    Jane    |   32    |
 |   March    |    John    |   35    |
 |   March    |    Alex    |   30    |

I want convert data to Json :

var data = [{
            "Month": "January",
            "Information": [{
              "Name": "John",
              "Sum": 25
            }]
          }, {
            "Month": "February",
            "Information": [{
              "Name": "Jane",
              "Sum": 30
            }, {
              "Name": "John",
              "Sum": 35
            }, {
              "Name": "Alex",
              "Sum": 20
            }]
          },{
            "Month": "March",
            "Information": [{
              "Name": "Jane",
              "Sum": 32
           }, {
              "Name": "John",
              "Sum": 35
           }, {
              "Name": "Alex",
              "Sum": 30
            }]
        }]

How to do this. I want Json to create stacked bar chart.js. thanks.

1
  • I use postgresql . Commented Aug 30, 2017 at 8:36

2 Answers 2

0

Here you go with a solution https://jsfiddle.net/mou882tw/

var data = [];
var lastMonth = "";

var child = document.getElementsByTagName('tr');
var temp = {};
var th = child[0].children;
for(var i=1; i<child.length; i++){
  var td = child[i].children;
  if(lastMonth != td[0].textContent){
  	if( JSON.stringify(temp) != '{}')
  		data.push(temp);
  	temp = {};
  	temp["Information"] = [];
  }

  var innerJSON = {};
  for(var j=0; j<td.length; j++){
  	if(j == 0){
    	temp[th[j].textContent] = td[j].textContent;
      lastMonth = td[j].textContent;
    } else {
    	innerJSON[th[j].textContent] = td[j].textContent;
    }
  }
  temp["Information"].push(innerJSON);
  if(i == (child.length -1)){
  	data.push(temp);
  }
}
console.log(data);
<table>
  <thead>
    <tr>
      <th>Month</th>
      <th>Name</th>
      <th>Sum</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>January</td>
      <td>John</td>
      <td>25</td>
    </tr>
    <tr>
      <td>February</td>
      <td>Jane</td>
      <td>30</td>
    </tr>
    <tr>
      <td>February</td>
      <td>John</td>
      <td>35</td>
    </tr>
    <tr>
      <td>February</td>
      <td>Alex</td>
      <td>20</td>
    </tr>
    <tr>
      <td>March</td>
      <td>Jane</td>
      <td>32</td>
    </tr>
    <tr>
      <td>March</td>
      <td>John</td>
      <td>35</td>
    </tr>
    <tr>
      <td>March</td>
      <td>Alex</td>
      <td>30</td>
    </tr>
  </tbody>
</table>

Hope this will help you.

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

5 Comments

@Jane.. Hope the answer will help you.
thanks for you answer but I wrong communication the topic to you . Data table mean Database table . I'm sorry very much, Bother you again, please.
@Jane please share database data, will convert it to proper JSON format.
database data it's same data table on top.
I mean, it should be in certain format, may be csv, text or some other format.
0

Any possibility of table to json but with nested json. How it will be formated

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.