'Convert AWS Lambda function JSON to array in ReactJS
I am attempting to return data from my AWS lambda function hitting a DynamoDB table and display it using ReactJS. It worked fine hard coding data in an array but not when I get live data.
Here is the code returning the data:
const response = await fetch('https://blah.amazonaws.com/blah');
const body = await response.json();
const body2 = JSON.stringify(body);
When I log body2 to the console, here is what I see:
{"body":"{"Items":[{"Vendor":"Hankook","Date":"08/20/2022","Id":100,"Amount":"18000","InvoiceNumber":"000"},{"Vendor":"Hankook2","Date":"08/22/2022","Id":102,"Amount":"2001","InvoiceNumber":"222"},{"Vendor":"Hankook3","Date":"08/23/2022","Desc":"This is a test","Id":103,"Amount":"3001","InvoiceNumber":"333"},{"Vendor":"Hankook1","Date":"08/21/2022","Id":101,"Amount":"9001","InvoiceNumber":"111"}],"Count":4,"ScannedCount":4}"}
Since my code expects an array to display it, I tried to convert it but it makes a massive array and I suspect it's due to the JSON having the "body" and "Items" and it can't handle it correctly:
const newArray = Array.from(body2);
newArray has a len of 551 and appears to be splitting it on every character.
How can I convert the above JSON to an array similar to this hard coded data that I was originally using?
[ { "id" : "100", "Vendor" : "Hankook", "Amount" : "$18,000", "Invoice" : "000", "Date" : "08/20/2022" }, { "id" : "101", "Vendor" : "Hankook1", "Amount" : "$9,001", "Invoice" : "111", "Date" : "08/21/2022" }, { "id" : "102", "Vendor" : "Hankook2", "Amount" : "$2,001", "Invoice" : "222", "Date" : "08/22/2022" } ]
Thanks!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
