Created
October 19, 2018 03:17
-
-
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
This file contains hidden or 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
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