Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

DynamoDB Json Formatting w/ Node.js with Lambda

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
        {
            "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?

>Solution :

You just need to return the items of body:

body = JSON.stringify(body.Items);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading