I have a json file in a sFTP server that I go grab using the ssh2-sftp-client library. I do NOT want to write this file to a local file, I want to be able to read it and save it into a JSON variable in code. But I cannot seem to convert the buffer output to JSON. Here's my code:
const Client = require('ssh2-sftp-client');
async function getFTPFile() {
const sftp = new Client();
const config = {
host: ...,
username: ...,
password: ...
};
return sftp.connect(config)
.then(() => sftp.get('./directory/Data.json'))
.then((data) => {
console.log(data);
return JSON.parse(data.toString());
});
}
I confirmed it's pulling the right file but when I try to convert the buffer array to a json object I get the following error:
SyntaxError: Unexpected token � in JSON at position 0
at JSON.parse (<anonymous>)
How do I resolve this error?
Turns out the file was encoded with utf16 and it started with a ZWNBSP character. So the following resolved the issue:
let file = data.toString('utf16le');
if (file.charCodeAt(0) === 0xFEFF) {
file = file.slice(1)
}
return JSON.parse(file);