Elias
Elias

Reputation: 53

Get json data from object from get Storage Amplify

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".

Upvotes: 0

Views: 1769

Answers (1)

Elias
Elias

Reputation: 53

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);
        });

Upvotes: 2

Related Questions