easy-konvertor v1.0.6
easy-konvertor
Ever had the urge to parse JSON to XML or CSV to JSON? And wanted to access the data in some sane, easy way? Then easy-konvertor is what you're looking for!
Description
Simple JSON to XML converter.
Installation
Simplest way to install easy-konvertor
is to use npm, just npm
install easy-konvertor
which will download easy-konvertor and all dependencies.
Usage
No extensive tutorials required because you are a smart developer! The task of parsing JSON should be an easy one, so let's make it so! Here's some examples.
Convert a JSON file into XML:
import { Convertor } from "easy-konvertor";
let xml = Convertor.readJson("<path_file>").convert(FileTypeEnum.XML);
console.log(xml);
Convert a pre-loaded JSON string
import { Convertor } from "easy-konvertor";
const json = `{
"employees": {
"employee": [{
"name": "John",
"surname": "Dalton",
"age": 40
},
{
"name": "Harry",
"surname": "McCormick",
"age": 45
}
]
}
`
let xml = Convertor.json2xml(json);
console.log(xml);
This should output the following XML document:
<employees>
<employee>
<name>John</name>
<surname>Dalton</surname>
<age>40</age>
</employee>
<employee>
<name>Harry</name>
<surname>McCormick</surname>
<age>45</age>
</employee>
</employees>
Use options for XML formatting
let options = {
padding: 2,
XMLHeader: true
}
Convertor.options(options).json2xml(json);
Options: | Parameter | Description | Default | |------------------|---------------------------------------------------------------------|---------| | XMLHeader | If true add the XML XMLHeader | false | | padding | Number of blank for indenting elements.If not present a tab is used | | | root | A string to wrap around the rendered XML document | |
Beautify existing XML string
import { Convertor } from "easy-konvertor";
let xml = `<employees>
<employee>
<name>John</name>
<surname>Dalton</surname>
<age>40</age>
</employee>
<employee>
<name>Harry</name>
<surname>McCormick</surname>
<age>45</age>
</employee>
</employees>`
let pad = " ";
xml = Convertor.beautifyXml(xml, pad)
Format XML with an optional pad
string. If not present a tab character is used.
The output xml is like this:
<employees>
<employee>
<name>John</name>
<surname>Dalton</surname>
<age>40</age>
</employee>
<employee>
<name>Harry</name>
<surname>McCormick</surname>
<age>45</age>
</employee>
</employees>
Convert a CSV into JSON
import { Convertor, FileTypeEnum } from "easy-konvertor";
//Convert a string read from a file
Convertor.readCsv("<file name>").convert(FileTypeEnum.JSON);
//Use 'csvHeader' if the header is not present or you want to override it
Convertor.csvHeader("name;lastname;age").readCsv("<file name>").convert(FileTypeEnum.JSON);
Convert a preloaded CSV string into JSON
import { Convertor, FileTypeEnum } from "easy-konvertor";
//if the header is not present or you want to override it
Convertor.csv2json("<csv string>");