I'm currently facing an issue while attempting to read and copy a JSON file uploaded by the user into an array. When using .readAsText(), the returned data includes string formatting elements like \" and \n.
Is there a way to utilize FileReader or any other method of file reading (excluding server-side processing) to extract the raw JSON without such additional characters?
For instance, rather than
[
{"hello": "world"}
]
or
[{"hello": "world"}]
We want it to display as
"[\n{\"hello\": \"world\"}\n]"
without extra escape characters?
Edit: I have researched the JSON.parse(text) method, but encountered an error when trying to parse the FileReader object:
let fileUploaded = new FileReader();
fileUploaded.readAsText(MY_JSON_FILE);
console.log(JSON.parse(fileUploaded));
The above code resulted in the following error message:
error TS2345: Argument of type 'FileReader' is not assignable to parameter of type 'string'
Is there a way to assign the content read by FileReader to a new variable as a string, which can then be parsed successfully?