npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

to_json

v1.1.1

Published

turn everything into json

Downloads

7

Readme

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!