Reputation: 475
Here I am calling a GetFile , getting response as ArrayBuffer{} object, In network Tab response is {"errors":["photoProof Image is not available in the system"]}, if I do response.errors=undefined.
$scope.getDocuments = function(){
Myservice.downLoadDocument('photo', $scope.user.mobileNo).
then(function(response){
})
}
If I Do this, getting below this value in Network Tab.
response.byteLength = 64
how do I convert this ArrayBuffer into proper JSON format?
Upvotes: 10
Views: 35467
Reputation: 143
You can use TextDecoder:
try {
parsedJson = JSON.parse(new TextDecoder().decode(response as ArrayBuffer));
} catch (e) {
parsedJson = {};
}
In my case the request is waiting for an ArrayBuffer response, but if an error occours on the backend, I get a JSON object containing the error. I check if the response is an error with this code.
The try-catch block is needed because if the ArrayBuffer is not a json, then the JSON.parse() method will throw an error.
Upvotes: 11
Reputation: 399
This wokrs for me :
String.fromCharCode.apply(null, new Uint8Array(replaceThisByYourData))
Upvotes: 7
Reputation: 339
You can use:
Buffer.from(yourArrayBufferValue).toJSON();
Although this answer is a little bit late, I hope it helps someone
Upvotes: 4
Reputation: 628
var jsonResult = JSON.parse(JSON.stringify(response));
will do wonders.
Upvotes: 2
Reputation: 31
Maby you can use json-bufferify. It's a module to help you convert between JSON and ArrayBuffer, and you can run it in both Node.js and browser.
Upvotes: 3