0

I'm trying to create a JSON from the data returned by DynamoDB. and I'm confused on how to create it.

Currently, I get data into an array and populate the JSON below is my code.

var res = {};
dynamodb.scan(params).promise().then(function (data) {
 var ingred = data.Items[0].ingredients;
 res = {
 [ingred]: {}
 };
 ingred.max = Math.max(...data.Items.map(({max}) => parseFloat(max))) + "";
 ingred.min = Math.min(...data.Items.map(({min}) => parseFloat(min))) + "";
 console.log(JSON.stringify(res, null, 4));
})

Here when I run this, I get the output as below.

{
 "a": {}
}
{
 "b": {}
}
{
 "c": {}
}

and when I change my code to the below

var res = {};
 dynamodb.scan(params).promise().then(function (data) {
 var ingred = data.Items[0].ingredients;
 console.log(ingred);
 ingred = {};
 ingred.max = Math.max(...data.Items.map(({ max }) => parseFloat(max))) + "";
 ingred.min = Math.min(...data.Items.map(({ min }) => parseFloat(min))) + "";
 console.log(JSON.stringify(ingred, null, 4));
 })

the output I get is as below

a
{
 "max": "14",
 "min": "8.5"
}
b
{
 "max": "1.98",
 "min": "0.37"
}
c
{
 "max": "155",
 "min": "6"
}

I am trying to achieve the result as below.

{
 "a": {
 "max": "14",
 "min": "8.5"
 },
 "b": {
 "max": "1.98",
 "min": "0.37"
 },
 "c": {
 "max": "155",
 "min": "6"
 }
}

please let me know on how I can achieve this.

Thanks

asked Jan 10, 2018 at 6:08
1
  • A clear sample of the data in data.Items[0] and another sample for the expected output will help people answer this question better. Commented Jan 10, 2018 at 6:23

4 Answers 4

1

You just need to set the ingred in the res

var res = {};
dynamodb.scan(params).promise().then(function (data) {
 var ingred = data.Items[0].ingredients;
 res[ ingred ] = {};
 res[ ingred ].max = Math.max(...data.Items.map(({ max }) => parseFloat(max))) + "";
 res[ ingred ].min = Math.min(...data.Items.map(({ min }) => parseFloat(min))) + "";
 console.log(JSON.stringify(res, null, 4));
})
answered Jan 10, 2018 at 6:16
Sign up to request clarification or add additional context in comments.

Comments

1

Try to do this:

var res = {};
dynamodb.scan(params).promise().then(function (data) {
 var ingred = data.Items[0].ingredients;
 res[ingred] = {};
 res[ingred].max = Math.max(...data.Items.map(({ max }) => parseFloat(max))) + "";
 res[ingred].min = Math.min(...data.Items.map(({ min }) => parseFloat(min))) + "";
 console.log(JSON.stringify(res, null, 4));
})
answered Jan 10, 2018 at 6:13

Comments

1

Add max and min to your object in res[ingred].

var res = {};
dynamodb.scan(params).promise().then(function (data) {
 var ingred = data.Items[0].ingredients;
 res[ingred] = {};
 res[ingred].max = Math.max(...data.Items.map(({ max }) => parseFloat(max))) + "";
 res[ingred].min = Math.min(...data.Items.map(({ min }) => parseFloat(min))) + "";
 console.log(JSON.stringify(res, null, 4));
})
answered Jan 10, 2018 at 6:15

Comments

0

Just store a and b into an object and store that into a global object:

 const result = {};
 dynamodb.scan(params).promise().then(function (data){
 const ingred = data.Items[0].ingredients;
 result[ingred] = {
 max: Math.max(...data.Items.map(({ max }) => parseFloat(max))),
 min: Math.min(...data.Items.map(({ min }) => parseFloat(min))),
 };
 })
answered Jan 10, 2018 at 6:23

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.