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

simple-get-json

v10.0.0

Published

getJSON function similar to that of jQuery's for Node or the browser

Downloads

83

Readme

npm Dependencies devDependencies

eslint badge eslint 3rd party badge

testing badge coverage badge

Known Vulnerabilities Total Alerts Code Quality: Javascript

Filesize badge (UMD) Filesize badge (ESM)

Licenses badge

(see also licenses for dev. deps.)

issuehunt-to-marktext

getJSON

getJSON function similar to that of jQuery's.

If no callback or error-back is provided (or when an array of URLs is supplied), a promise will be returned. If an array is provided along with a callback, that callback will be executed before the promise is resolved.

You may use the file index-es.js to use the ES2017 await keyword for Promise results, as well as take advantage of ES6 Module import:

try {
  const [urlObj1, urlObj2] = await getJSON([url1, url2]);
} catch (err) {
  // Handle errors here
}

simple-get-json also accepts an array of URLs (waiting for all to load):

const [obj1, obj2] = await getJSON([url1, url2]);
// Do something with "obj1" and "obj2"

Alternatively, you can use regular then Promises:

getJSON([url1, url2]).then(function (objsArr) {
  // Do something with "objsArr" array
}, function (err) {
  // Handle any errors here
  console.log('err', err);
});

Or use the old callback style.

getJSON(url, function (data) {
  // Do something with "data"
});

An optional third argument can be provided as an error-back (which will be supplied the error message and originally supplied URL).

Install

npm install simple-get-json

Setup

Browser

<!--
For older browser support
<script src="node_modules/core-js-bundle/minified.js"></script>
-->
<script src="node_modules/simple-get-json/dist/index.js"></script>
getJSON(...args);

or for ESM:

  1. Direct use:
import {getJSON} from './node_modules/simple-get-json/dist/index-es.js';
  1. With a bundler:
import {getJSON} from 'simple-get-json';

Node (CJS)

const {getJSON} = require('simple-get-json');

See below for buildGetJSON usage.

Node (ESM)

In ESM Node, you can:

  1. Directly import the module (for use relative to the current working directory):
import {getJSON} from 'simple-get-json';
  1. Build a version of getJSON which works relative to the current file (or some other URL):
import {buildGetJSON} from 'simple-get-json';

const getJSON = buildGetJSON({
  baseURL: import.meta.url
});

OR:

  1. Build a version of getJSON which works relative to a specific file directory:
import {buildGetJSON} from 'simple-get-json';

const getJSON = buildGetJSON({
  cwd: '/some/current/working/directory'
});

Todo

  • Support named parameters ala jQuery
  • Support rest of jQuery API
  • Make local or URL loading optional for Node

See also