Parse a Json(with array and objects) and export the data into Excel file in Node.js Parse a Json(with array and objects) and export the data into Excel file in Node.js json json

Parse a Json(with array and objects) and export the data into Excel file in Node.js


You can do it with AlaSQL javascript library with special SEARCH operator, which is designed to do search for nested objects. See the code below, which generates the Excel file with your structure (I did not include all fields):

var alasql = require('alasql');var data = [{   "id": 1255,   "title": "The Brain and Nervous System (LS1.D)",   // ...},{   "id": 1256,    // ...}];// Here is search queryalasql('SEARCH / AS @a \    UNION ALL( \      introduction AS @b \      resources / AS @c \      RETURN(@a->id AS id, @a->title AS title, @a->description AS description, \        @a->keyTerms AS keyTerms, @a->visible AS visible, \        @b->id as [introduction.id], @b->title as [introduction.title], \        @c->id AS [introduction.resources.id], \        @c->contentItem->id AS [introduction.resources.contentItem.id] \      ) \    , \      objectives AS @b \      resources / AS @c \      RETURN(@a->id AS id, @a->title AS title, @a->description AS description, \        @a->keyTerms AS keyTerms, @a->visible AS visible, \        @b->id as [objectives.id], @b->title as [objectives.title], \        @c->id AS [objectives.resources.id], \        @c->contentItem->id AS [objectives.resources.contentItem.id] \      ) \    ) INTO XLSX("test411.xlsx",{headers:true})\    FROM ?',[data]);

Some explanations:

  • SEARCH - is a special statement for query nested objects
  • / - loop over array elements
  • UNION ALL(...,...) - union of all nested found records
  • AS @variable - save current search position to temporary variable
  • introduction - go deep into the property "introduction"
  • RETURN(...,...) - create a record (JSON object) with properties
  • RETURN(value AS alias) - alias for the value
  • @a->id - get id property similar to a.id in JavaScript
  • [...] - terms with any special charaters
  • INTO XLSX("test411.xlsx",{headers:true}) - save results to Excel file with headers
  • FROM ? - get data from parameter
  • alasql(sql, [data]) - put data variable as first parameter of the query

You also need to add other columns to finish this query. The sample file will be uploaded here in two hours.

You also can remove this line from the source code: INTO XLSX(), and alasql() will return a JSON object with all properties:

var res = alasql('SEARCH / AS @a UNION ALL(...) FROM ?',[data]);console.log(res);

Here is the jsFiddle example


You are using the json2xls module which accepts either on object or an array, but nested level object structure. What I mean is that your definition would be fine if introduction and objectives were scalar properties. I.e.

{    "id": 1255,    "title": "...)",    "description": "...",    "keyTerms": "...",    "visible": true,    "introduction": "string/int/float/bool/date",    "objectives": "string/int/float/bool/date"}

or

[{    "id": 1255,    "title": "...)",    "description": "...",    "keyTerms": "...",    "visible": true,    "introduction": "string/int/float/bool/date",    "objectives": "string/int/float/bool/date"},{    "id": 1256,    "title": "...)",    "description": "...",    "keyTerms": "...",    "visible": true,    "introduction": "string/int/float/bool/date",    "objectives": "string/int/float/bool/date"}]

but in your case introduction is an object with nested elements, and objectives is an array of objects, which both are interpreted as [object] [object]

I do not know what you want in your excel file, but you need to decide how to flatten the structure first.