0

I've got an issue here with formatting my json and I can't quite figure out how to do it. when I run this code via Lambda:

  let body;
  let statusCode = 200;
  const headers = {
    "Content-Type": "application/json"
  };
body = await dynamo.scan({ TableName: "MyTutorials" }).promise();
body = JSON.stringify(body);

It returns as json string that looks like this:

{
    "Items": [
        {
            "published": false,
            "description": "This is an orange.",
            "id": 2,
            "title": "Orange"
        },
        {
            "published": true,
            "description": "this is an apple",
            "id": 1,
            "title": "Apple"
        }
    ],
    "Count": 2,
    "ScannedCount": 2
}

Thats great and all, but for my React app I need something cleaner that looks like this:

[
        {
            "published": false,
            "description": "This is an orange.",
            "id": 2,
            "title": "Orange"
        },
        {
            "published": true,
            "description": "this is an apple",
            "id": 1,
            "title": "Apple"
        }
    ]

Bascially, I need just the straight-up array in the json and not wrapped with all the other stuff like "Items" and "Count." Any ideas on how to make that call?

1 Answer 1

1

You just need to return the items of body:

body = JSON.stringify(body.Items);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.