Skip to content

Instantly share code, notes, and snippets.

@froi
Created October 19, 2018 03:17
Show Gist options
  • Save froi/d04a7379dc9840556dcea6f6e093e4bb to your computer and use it in GitHub Desktop.
Save froi/d04a7379dc9840556dcea6f6e093e4bb to your computer and use it in GitHub Desktop.
Small snippet to parse json string that could be formatted in utf16le and still be able to consume json strings in utf8
const fetch = require('node-fetch');
const encoding = require('encoding');
fetch('https://open.gsa.gov/code.json')
.then(response => response.buffer())
.then(json => {
let text = {};
if(json.indexOf('\uFEFF', 0, 'utf16le') === 0 ) {
text = encoding.convert(json, 'utf8', 'utf16le');
} else {
text = json.toString();
}
console.log(JSON.parse(text));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment