Michael
Michael

Reputation: 179

getting all array property data using alasql

I have here a JSON object having an array of data. My issue is how could I get the array data to export it to excel? I'm currently using alaSql for this.

{
 a: 'test',
 b: [
     {c:'test1',
      d: 'test2'},
     {c:'test2', d: 'test1'}]
}

Upvotes: 1

Views: 1291

Answers (1)

agershun
agershun

Reputation: 4107

To traverse nested JSON object you need to use AlaSQL's SEARCH operator like below:

var data = [
{
   a: 'test',
   b: [
       {c:'test1',
        d: 'test2'},
       {c:'test2', d: 'test1'}]
  },
 {
   a: 'testB',
   b: [
       {c:'test3',
        d: 'test4'},
       {c:'test5', d: 'test6'}]
  }
];

var res = alasql('SEARCH / AS @p b / CLONEDEEP() SET(a=@p->a) \
   INTO XLSX("restest390a.xlsx",{headers:true}) FROM ?',[data])

In this statement AlaSQL:

  • traverses over all elements of source object,
  • then saves each element into temporary variable @p
  • then looks into 'b' property of element
  • clones record to prevent changes in the original data
  • set property a of current record to property a of saved variable @p
  • save all records into Excel file

Upvotes: 1

Related Questions