2017-05-11 5 views
0

kurzem BeispielAlle bekannten Arten CSV-Arrays zu JSON-Dateien mit Knoten zu konvertieren und Kanten

CSV-Datei:

name, source, target 
John , A , None 
Emma , B , A 
Mike , C , A 

theoretische entsprechenden JSON-Datei:

{ 
    "comment": "example code", 
    "nodes": [ 
    { 
     "name": John, 
     "source" : A 
    }, 
    { 
     "name": Emma , 
     "source" : B 
    }, 
    { 
     "name": Mike , 
     "source" : C 
    } 
    ], 
    "links": [ 
    { 
     "source": B, 
     "target": A 
    }, 
    { 
     "source": C, 
     "target": A 
    } 
    ] 
} 

resultierende Graph Konzept:

B -> A <- C 

Die obige JSON-Datei ist nicht absolut. Es kann eine Vielzahl von Schemas haben. Das obige Beispiel ist nur sehr einfach.

Ich hoffe, es gibt eine Software oder Bibliothek, die CSV-Array zu JSON-Dateien oder etwas in dieser Richtung leicht drehen kann.

Ich habe viele CSV-Dateien, die ich in JavaScript-Grafiken konvertieren möchte, aber zuerst brauche ich eine JSON-Eingabedatei, die Daten gut strukturiert hat. Ich möchte JSON Dateiaufbau automatisieren.

Antwort

0

Hier ist ein Beispiel mit csvtojson.

const csv = require('csvtojson'); 

const csvFilePath = 'test.csv'; 
var data = []; 

csv().fromFile(csvFilePath) 
    .on('json', (jsonObj) => { 
     data.push(jsonObj); 
    }) 
    .on('done', (error) => { 
     if (error) { 
      console.error(error); 
     } else { 
      formatData(data); 
     } 
    }); 

function formatData(data) { 

    var formatted = { 
     comment: "hello", 
     nodes: [], 
     links: [] 
    }; 

    data.forEach(function(r){ 

     //check for your empty or 'None' fields here 
     if(r.name && r.source){ 
      formatted.nodes.push({ name: r.name, source: r.source }); 
     } 

     if (r.source && r.target) { 
      formatted.links.push({ source: r.source, target: r.target }); 
     } 
    }); 

    //do something with the finished product 
    console.log(formatted); 
} 
0

Hier ist eine Lösung jq

Wenn die Datei filter.jq enthält

[ 
    split("\n")             # split string into lines 
| [.[0] | split(",")[] | gsub("[[:space:]]+";"")] as $headers # split header 
| (.[1:][] | split(","))           # split data rows 
| select(length>0)            # get rid of empty lines 
| [ [ $headers, map(gsub("[[:space:]]+";"")) ]     # 
    | transpose[]            # assemble objects 
    | {key:.[0], value:.[1]}          # from keys and values 
    ] | from_entries            # 
] 
| { 
    "comment":"example code",         # convert objects 
    "nodes": map({name,source}),         # to requested format 
    "links": map(select(.target!="None")|{source,target})  # 
    } 

und data enthält

name, source, target 
John , A , None 
Emma , B , A 
Mike , C , A 

dann der Befehl

mit 10
jq -M -R -s -r -f filter.jq data  

produzieren

{ 
    "comment": "example code", 
    "nodes": [ 
    { 
     "name": "John", 
     "source": "A" 
    }, 
    { 
     "name": "Emma", 
     "source": "B" 
    }, 
    { 
     "name": "Mike", 
     "source": "C" 
    } 
    ], 
    "links": [ 
    { 
     "source": "B", 
     "target": "A" 
    }, 
    { 
     "source": "C", 
     "target": "A" 
    } 
    ] 
} 
Verwandte Themen