2015-12-30 10 views
9

Sono nuovo su Node.js. Il mio requisito è, ho bisogno di analizzare un JSON ed esportare i dati in file Excel con tutti i campi nel JSON.Parse a Json (con array e oggetti) ed esporta i dati nel file Excel in Node.js

mio JSON è la seguente:

{ 
    "id": 1255, 
    "title": "The Brain and Nervous System (LS1.D)", 
    "description": "By the time you finish this playlist, you should be able to: 1. Describe how the nervous system is organized and how it works 2. Describe the role of the nervous sytem, and explain how our different senses work to send information to your brain", 
    "keyTerms": "You should also be able to define the following words: stimulus, response, neuron, sensory neuron, motor neuron, nerve impulse, dendrite, axon, nerve, central nervous system, peripheral nervous system, brain, spinal cord, cerebrum, cerebellum, brain stem, retina, cochlea", 
    "visible": true, 
    "introduction": { 
    "id": 5336, 
    "title": "Introductory Materials", 
    "resources": [ 
     { 
     "id": 23022, 
     "title": "Vocabulary - Brain and Nervous System", 
     "description": "", 
     "purpose": "", 
     "category": "Website", 
     "position": 1, 
     "contentItem": { 
      "id": 1650, 
      "url": "http://quizlet.com/45497180/flashcards", 
      "itemType": "Website", 
      "embedUrl": null 
     } 
     }, 
     { 
     "id": 23023, 
     "title": "The Brain and Nervous System Study Guide", 
     "description": "Fill out this study guide while studying! It will help you prepare for the assessment!", 
     "purpose": "", 
     "category": "Website", 
     "position": 2, 
     "contentItem": { 
      "id": 12581, 
      "url": "https://docs.google.com/a/summitps.org/document/d/1TjF1MY3cyGNKT4s46uk1iz5NvjrY59eNPH8YKYYTC_E/edit", 
      "itemType": "Website", 
      "embedUrl": null 
     } 
     } 
    ] 
    }, 
    "objectives": [ 
    { 
     "id": 10732, 
     "title": "1. Describe how the nervous system is organized", 
     "caContribution": 5, 
     "position": 1, 
     "resources": [ 
     { 
      "id": 23024, 
      "title": "Reading - How the Nervous System Works", 
      "description": "", 
      "purpose": "", 
      "category": "Document", 
      "position": 1, 
      "contentItem": { 
      "id": 1651, 
      "url": null, 
      "itemType": "Document", 
      "embedUrl": "https://view-api.box.com/1/sessions/493fca96d46a4559813c3118ebeef8b6/view?theme=light", 
      "s3Url": "/files/content_items/relateds/000/001/651/original/53d1ddd8f07787731aa7d84f-how_20nervous_20system_20works_001.pdf?1424368501" 
      } 
     } 
     ] 
    }, 
    { 
     "id": 10734, 
     "title": "2. Describe the role of the nervous sytem, and explain how our different senses work to send information to your brain", 
     "caContribution": 5, 
     "position": 2, 
     "resources": [ 
     { 
      "id": 23039, 
      "title": "Study Jams - The Senses", 
      "description": "This series of videos explains the different senses: sight, hearing, taste, touch, and smell", 
      "purpose": "", 
      "category": "Website", 
      "position": 1, 
      "contentItem": { 
      "id": 1666, 
      "url": "http://studyjams.scholastic.com/studyjams/jams/science/human-body/touching.htm", 
      "itemType": "Website", 
      "embedUrl": null 
      } 
     }, 
     { 
      "id": 23040, 
      "title": "Nervous System: I'm Sensing Something", 
      "description": "Format: Article Content: How does the nervous system work and interact with other systems", 
      "purpose": "", 
      "category": "Website", 
      "position": 1, 
      "contentItem": { 
      "id": 12582, 
      "url": "http://www.biology4kids.com/files/systems_nervous.html", 
      "itemType": "Website", 
      "embedUrl": null 
      } 
     } 
     ] 
    } 
    ] 
} 

Ho usato il modulo json2xls e ho avuto il foglio di calcolo con i campi id, titolo, descrizione, fattori chiave, visibile, introduzione, obiettivi dove venivano introduzione e obiettivi come [oggetto] [oggetto], [oggetto] [oggetto]

Ecco un codice per esportare in Excel File:

var fs = require("fs"); 
var json2xls = require("json2xls"); 

fs.readFile('/home/e100093/nodejs/sampleJson.json','utf8',function(err,body){ 

var jsonData = JSON.parse(body); 

var xls = json2xls(jsonData); 
fs.writeFileSync('final-test.xlsx', xls, 'binary'); 

}); 

Ma voglio i campi di output come id, titolo, descrizione, keyTerms, visible, introduction.id, introduction.title, introduction.resources.id, introduction.resources.title, ..., objectives.id, obiettivi. titolo, obiettivi.descrizione, ....., ecc.

Ho bisogno di analizzare l'oggetto e la matrice dinamicamente e produrre i campi in ogni colonna del foglio di calcolo.

L'output di esempio con colonne e dei dati sono i seguenti:

id title description keyTerms visible introduction.id introduction.title introduction.resources.id introduction.resources.title introduction.resources.description introduction.resources.purpose introduction.resources.category introduction.resources.position introduction.resources.contentItem.id introduction.resources.contentItem.url introduction.resources.contentItem.itemType introduction.resources.contentItem.embedUrl objectives.id objectives.title objectives.caContribution objectives.position objectives.resources.id objectives.resources.title objectives.resources.description objectives.resources.purpose objectives.resources.category objectives.resources.position objectives.resources.contentItem.id objectives.resources.contentItem.url objectives.resources.contentItem.itemType objectives.resources.contentItem.embedUrl objectives.resources.contentItem.s3Url 

1255 The Brain and Nervous System (LS1.D) By the time you finish... You should also... true 5336 Introductory Materials 23022 Vocabulary - Brain and Nervous System null null Website 1 1650 http://quizlet.com/... Website null null null null null null null null null null null null null null null null 
1255 The Brain and Nervous System (LS1.D) By the time you finish... You should also... true 5336 Introductory Materials 23023 The Brain and Nervous System Study Guide Fill out this... null Website 2 12581 https://docs.google.com/... Website null null null null null null null null null null null null null null null null 
1255 The Brain and Nervous System (LS1.D) By the time you finish... You should also... true null null null null null null null null null null null null 10732 1. Describe how... 5 1 23024 Reading - How... null null Document 1 1651 null Document https://view-api.box.com/1... /files/content... 
1255 The Brain and Nervous System (LS1.D) By the time you finish... You should also... true null null null null null null null null null null null null 10734 2. Describe the role... 5 2 23039 Study Jams - The Senses This series of videos null Website 1 1666 http://studyjams.scholastic.com... Website null 
1255 The Brain and Nervous System (LS1.D) By the time you finish... You should also... true null null null null null null null null null null null null 10734 2. Describe the role... 5 2 23040 Nervous System: I'm Sensing... Format: Article Content... null Website 1 12582 http://www.biology4kids.com... Website null 

Please help me come fare questo.

+0

Si prega inoltre di fornire il codice che si è usando per creare il file excel. http://stackoverflow.com/help/how-to-ask – MojoJojo

+0

@MojoJojo sì, ho aggiunto il codice che ho usato per esportare i dati JSON. – Arunraj

+0

Fornire un esempio dei dati che si desidera ottenere. –

risposta

0

Si sta utilizzando il modulo json2xls che accetta sia su oggetto o array, ma struttura ad oggetti di livello nidificati. Ciò che intendo è che la tua definizione andrebbe bene se introduzione e obiettivi fossero proprietà scalari. Cioè

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

o

[ 
{ 
    "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" 
} 
] 

ma nel caso introduzione è un oggetto con elementi nidificati, e obiettivi è un array di oggetti, che entrambi vengono interpretati come [oggetto] [oggetto]

Non so cosa vuoi nel tuo file excel, ma devi decidere come appiattire prima la struttura.

5

È possibile farlo con la libreria javascript AlaSQL con l'operatore speciale SEARCH, progettato per eseguire la ricerca di oggetti nidificati. Vedere il codice qui sotto, che genera il file Excel con la struttura (non ho incluso tutti i campi):

var alasql = require('alasql'); 

var data = [{ 
    "id": 1255, 
    "title": "The Brain and Nervous System (LS1.D)", 
    // ... 
}, 
{ 
    "id": 1256, 
    // ... 
}]; 

// Here is search query 
alasql('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]); 

Alcune spiegazioni:

  • RICERCA - è una dichiarazione speciale per ricerca annidati oggetti
  • /- ciclo su elementi di matrice
  • UNION ALL (..., ...) - unione di tutti i record trovati nidificate
  • AS @variable - salva la posizione di ricerca corrente per temporanea variabile
  • introduzione - andare in profondità nella proprietà "introduzione"
  • RITORNO (..., ...) - creare un record (oggetto JSON) con proprietà
  • ritorno (valore come alias) - alias per il valore
  • @ a-> id - ottenere la proprietà id simile a a.id in JavaScript
  • [.. .] - termini con eventuali caratteri speciali
  • INTO XLSX ("test411.xlsx", {intestazioni: vero}) - salva i risultati nel file Excel con intestazioni
  • DA? - ottenere i dati dal parametro
  • alasql (SQL, [i dati]) - inserire dati variabili come primo parametro della query

È inoltre necessario aggiungere altre colonne per finire questa query. Il file di esempio verrà caricato here in due ore.

È inoltre possibile rimuovere questa riga dal codice sorgente: INTO XLSX() e alasql() restituirà un oggetto JSON con tutte le proprietà:

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

Ecco la jsFiddle example