question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

xlsx to nested JSON

See original GitHub issue

Is it possible to read a xlsx file and convert it to a nested JSON? (i’m using node.js)

For example, this table:

Name | City | Country Jon | New York | USA Jon | Kansas | USA Peter | Nevada | USA

to this JSON:

{
   "Name": "Jon",
   "Addresses": [
              {"City": "New York", "Country": "USA"},
              {"City": "Kansas", "Country": "USA"}
           ]
},
{
   "Name": "Peter",
   "Addresses": [
              {"City": "Nevada", "Country": "USA"}
	  ]
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
reviewhercommented, Sep 13, 2021

As @RandomFractals discussed, there’s no simple general solution but you can always massage the output of sheet_to_json e.g.

var wb = XLSX.readFile("file.xlsx");
var data = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);
var result = data.reduce((acc, row) => {
  var target = acc.find(x => x.Name == row.Name);
  if(!target) acc.push((target = {Name: row.Name, Addresses: [] }));
  target.Addresses.push({City: row.City, Country: row.Country});
}, []);
2reactions
ahmad-reza619commented, Aug 30, 2019

OK thanks for explanation 👍 @RandomFractals

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to convert Excel to JSON - Easy Data Transform
Excel and JSON are very different formats, but you can quickly convert Excel .xlsx or .xls files to JSON (including nested JSON) with...
Read more >
xlsx to nested JSON · Issue #1591 - GitHub
otherwise, just transform the json you get from xlsx sheet to json to the structure you prefer looping over json array records and...
Read more >
How to convert a nested json object into an excel table using ...
writeFile(workBook, 'Tests.xlsx');. But quite expectedly it doesn't process the nested object the way I would like it to. I can't ...
Read more >
How can I create a nested JSON from an Excel- or CSV-file?
stefanh asked a question. ... How can I create a nested JSON from an Excel- or CSV-file? ... The attributes can be adjusted...
Read more >
Read from Multiple Excel Tables to Nested JSON
I have a connector that I have no problem working with to populate the container objects using List rows present and then apply...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found