to_json

1.1.1 • Public • Published

logo

to_json

Turn any data into JSON with a uniform interface!

Just call to_json() on any data with its data format as argument to get the parsed JSON. As simple as str.to_json('html'), str.to_json('csv'), str.to_json('xml'), str.to_json('yaml'), str.to_json('markdown'), etc.


why?

Our goal with Jasonette is to express all kinds of app logic in JSON. But not all existing data is in JSON. Also not everyone likes writing JSON.

What if you could take any data format and simply run str.to_json("markdown") to get a parsed JSON?


demo

Try it out at toJSON.co


features

The goal of this library is to function as a one-stop interface. It does not handle any conversion logic itself but utilizes other popular conversion libraries as dependency.

Currently supports:

data type command
atom str.to_json('feed')
csv str.to_json('csv')
cson str.to_json('cson')
hjson str.to_json('hjson')
html str.to_json('html')
markdown str.to_json('markdown')
rdf str.to_json('rdf')
rss str.to_json('rss')
svg str.to_json('svg')
xml str.to_json('xml')
yaml str.to_json('yaml')

Missing a cool data format? Contribute!


installation

npm install to_json

usage

There are two types: synchronous and asynchronous.

1. synchronous

Most to_json adapters are synchronous. For these, you simply call to_json and use the return value.

require('to_json');
var data = "<html><body><h1>hello</h1><p>World</p></body></html>";
var json = data.to_json('html');
console.log(json);

2. asynchronous

A few adapters (csv and feed) are asynchronous because the underlying library is asynchronous. For these just call to_json and pass a callback which will be triggered with the result.

require('to_json');
var data = "age, sex, location\n1,male,home\n20,female,nightclub\n30,male,work";
data.to_json('csv', function(json){
  console.log(json);
});

example

Markdown to JSON

var json = str.to_json('markdown');
console.log(json);

CSV to JSON

str.to_json('csv', function(json){
  console.log(json);
});

CSON to JSON

var json = str.to_json('cson');
console.log(json);

HJSON to JSON

str.to_json('hjson');

ATOM/RSS to JSON

str.to_json('feed');

HTML to JSON

This adapter uses the cheerio library to transform HTML into an object.

str.to_json('html');

XML to JSON

str.to_json('xml');

SVG to JSON

str.to_json('svg');

advanced

Sometimes you don't need all the data formats. Let's say you ONLY want to use this for CSV.

Simply initialize by calling the init method with a subset of adapters as can be seen in adapters.js

require('to_json').init({
  csv: require('./adapters/csv')
});
var data = "age, sex, location\n1,male,home\n20,female,nightclub\n30,male,work";
data.to_json('csv', function(json){
  console.log(json);
});

contribute

Feel free to send pull requests if you have improvements, bug fixes, or wrote any additional adapters.

If you have a suggestion for a new data type support, here's how to write an adapter:

  1. Write an adapter function (Look under adapters folder to see how other types work, and just add another there)
  2. Add an entry under src/adapters.js registry.
  3. Send a PR!

Readme

Keywords

none

Package Sidebar

Install

npm i to_json

Weekly Downloads

1

Version

1.1.1

License

ISC

Last publish

Collaborators

  • gliechtenstein