Polymer Web Component for transforming any text data to JSON:
Basically a web component wrapper over Kash Nouroozi’s CSV.js library.
Add the library using the Javascript package manager Bower:
bower install --save transform-to-json
Import Web Components’ polyfill:
<script src="bower_components/webcomponentsjs/webcomponents.min.js"></script>
Import Custom Element:
<link rel="import" href="bower_components/transform-to-json/transform-to-json.html">
Start using it!
<transform-to-json>
<!-- columns go here -->
<column></column>
<!-- data can go here or you can use AJAX -->
</transform-to-json>
| Attribute | Type | Default | Description |
|---|---|---|---|
input |
string | null |
Input to parse (can be populated via url or inlined) |
url |
string | null |
URL of input |
json |
object | null |
Parsed output |
format |
string | csv | Format of input, allowed: csv, tsv, ssv, |
trim |
boolean | false |
If true, output will be trimmed right to remove trailing spaces and newlines |
array |
boolean | false |
If true, output JSON array rather than JSON object (will drop property names) |
firstrownames |
boolean | false |
If true, first line of data is assumed to contain the names of the columns. Use Column Definitions to specify names if not included. |
Any non-whitespace, non-commented text within the <transform-to-json> nodes will be treated as input.
jsonchanged will fire whenever json changes. Useful whenever url is defined as input will be loaded asynchronously via AJAX.
Column fields can be named and parsed by defining <column> elements inside the <transform-to-json> HTML element.
| Attribute | Type | Description |
|---|---|---|
name |
string | Name of column field. If columns are unspecified by either a firstrownames or column definitions, JSON output will be disabled. |
type |
string | Parse as type: String,String?,Number,Number?,Integer?, or Boolean are allowed. |
There is more documentation on column.
Notes:
type can result in the same column having mixed types (ie: both string and numerical fields) in different rows.For detailed changelog, check Releases.
MIT License © Steven Skelton