0

I have some data from an object from an S3 Bucket via Amplify.

export function amplify() {
  let key = "68a92d44-f25a-4bd8-9543-cc95369ae9a0";
  return Storage.get(key + ".json", {
    download: true
  })
    .then(function(result) {
      return result;
    })
    .catch(err => console.log(err));
}

And the resulting data looks like this:

Object { LastModified: Date Tue Nov 12 2019, ETag: "\"(lotsofnumbers)\"", ContentType: "application/json", Metadata: {}, Body: Uint8Array(4168) }

How would I go about getting the JSON data from this object?

I have tried result.Body.toString() which gives me the JSON file and its content, but I cannot write result.Body.toString().name or .meta (the content in my jsonfile), for example, it gives me "undefined". I have also tried to convert the Uint8Array to JSON with parse, whereas I get this error: "JSON.parse: unexpected character at line 1 column 1 of the JSON data".

Elias
  • 53
  • 1
  • 9
  • you can take a look at this: https://stackoverflow.com/questions/51658835/converting-a-response-buffer-to-json/51659985#51659985 – aravind_reddy Nov 19 '19 at 10:34
  • I have tried that, but it did not work for me. I get the "JSON.parse: unexpected character at line 1 column 1 of the JSON data" error again – Elias Nov 19 '19 at 12:18

1 Answers1

2

This worked for me, don't know what was wrong, but now it works :)

  var obj = amplify();
        obj.then(async function(result) {
          var json = await new Response(result.Body).json();
          console.log(json.meta);
        });
Elias
  • 53
  • 1
  • 9