Last active
March 6, 2018 21:46
-
-
Save rfmcnally/18f23581bdf25023e8c2f44d37dbc059 to your computer and use it in GitHub Desktop.
Sample JS code for processing NDJSON object from Import.io API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is a simple approach to downloading the data from our API | |
// and processing it as JSON Object. This method is only sufficient for | |
// small API responses (should work for responses below 100k results). | |
// If your response contains more results, try having a look at this Stack Overflow QA: | |
// http://stackoverflow.com/questions/15121584/how-to-parse-a-large-newline-delimited-json-file-by-jsonstream-module-in-node-j | |
// We start by getting data from our API: | |
let ExtractorGuid =0000000-0000-0000-0000-000000000000 // You can find this in the URL bar on the dashboard following https://dash.import.io/ 0000000-0000-0000-0000-000000000000 | |
let apiKey = "YOUR_API_KEY"; // You can get it here https://import.io/data/account/ | |
let API_ENDPOINT = "https://data.import.io/extractor/${ExtractorGuid}/json/latest?_apikey=${apiKey}"; | |
let response; | |
// We are using the Fetch API, that is only available in newer browsers (details: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API). Try this code in Newest Chrome Console. | |
fetch(API_ENDPOINT) | |
.then(resp=> { | |
response = resp; | |
return response.text(); | |
}).then(responseBody=> { | |
if(!response.ok) { | |
throw responseBody; | |
} | |
return responseBody; | |
}) | |
.then(txt=> { | |
constjsonArray=JSON.parse(`[${txt}]`); | |
console.log('First ten or less results: '+JSON.stringify(jsonArray.slice(0, 10))); | |
console.log('Total number of results: '+jsonArray.length); | |
}) | |
.catch(error=>console.error(error)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment