r/javascript • full stack :hamster: • Jul 09 '17

We wrote a fast JSON/array fetcher and processor, thoughts?

https://www.npmjs.com/package/urljson
0 Upvotes

8 comments sorted by

15

u/realPubkey Jul 09 '17

I dont understand how this is usefull. Cant I just use fetch together with JSON.parse?

10

u/0x13mode Jul 09 '17

Ok. But why? This is solved problem for years.

Besides it's inferior solution because it mixes concerns (fetching file from the network and parsing contents). It doesn't allow for example parsing XML (or other format than JSON). Your solution is even bad in parsing JSON (you don't give possibility for passing reviver parameter to JSON.parse).

1

u/cgijoe_jhuckaby NaN Jul 09 '17

I highly recommend got from the fabulous sindresorhus. JSON fetch example:

const got = require('got');
got("http://echo.jsontest.com/key/value/one/two", { json: true })
    .then(response => {
        console.log(response.body); // parsed JSON
    });

1

u/spooky___ghost Jul 10 '17

Sorry but this is completely pointless. Looks like it only works in node and is a thin wrapper around http/https anyway.

https://github.com/HX3/urlJson/blob/master/index.js

your library

urljson.get('http://website.com/ex.json', false).then(function(result) {
    console.log('RESULT => ', result, '<= Formatting (2) =>');
}, function(error) {
    console.log(error);
});

fetch api (html standard)

fetch('http://website.com/ex.json').then(resp => resp.json())
    .then(result => console.log('RESULT => ', result);

1

u/GitHubPermalinkBot Jul 10 '17

I tried to turn your GitHub links into permanent links (press "y" to do this yourself):


Shoot me a PM if you think I'm doing something wrong. To delete this, click ^here.

1

u/[deleted] Jul 09 '17

Why does it use promises and callbacks at the same time?

1

u/Dragory Jul 09 '17

No it doesn't? The two callbacks are the usual onFulfilled and onRejected functions passed to then.

1

u/[deleted] Jul 09 '17

My bad..