简体   繁体   中英

converting array into json format in nodejs

Hi i have a list which consists of data and i want this data in particular json format as follows:

[
  {
    "slideName": "s0",
    "imageUrl":
      "https://s3.amazonaws.com/lifestyle345/testing/slides/cbaa5e650152a0332b494f0074985b6e-0.png",
    "txtUrl":
      "https://s3.amazonaws.com/lifestyle345/testing/speeches/virtualReality.txt"
  }
]

following is code :

var list = [];
var AWS = require('aws-sdk');

//var oldPrefix = 'texts/';
var s3 = new AWS.S3({params: {Bucket: 'lifestyle345'}});
exports.handler = (event, context, callback) => {
    function listAllKeys(s3bucket, start, end) {
        s3.listObjects({
            Bucket: s3bucket,
            Marker: start,
            MaxKeys: 1000,
        }, function(err, data) {
            if (data.Contents) {
                //console.log("Length" +data.Contents.length)
                for (var i = 0; i < data.Contents.length; i++) {
                    var key = "https://s3.amazonaws.com/lifestyle345/" +
                    data.Contents[i].Key;  //See above code for the structure of data.Contents
                    //console.log("KEY =" +key);
                    if (key.substring(0, 19) != end) {
                        list.push(key);
                    } else {
                        break;   // break the loop if end arrived
                    }
                }
                console.log(list);
                var jsonString = JSON.stringify(list );
                //console.log('Total - ', list.length);
                console.log(jsonString);
            }
        });
    }

    listAllKeys('lifestyle345', 'testing/slides', 'testing/speeches');

}

generated output:

' https://s3.amazonaws.com/lifestyle345/testing/slides/cbaa5e650152a0332b49400074985b6e-0.png ', ' https://s3.amazonaws.com/lifestyle345/testing/slides/cbaa5e650152a0332b494f0074985b6e-1.png ',

you can do the following

 console.log(JSON.stringify(list, undefined, 2));

the first parameter stringify the list, the second I honestly don't know what does it do but you have to include it, and the third is the number of indentations you need in your format.

You're only pushing raw values in the list array. You might want to either push ready-made objects right there in listAllKeys function, or create your objects once you're done with the list. Judging by your naming (slideName, imageUrl and textUrl), it seems as if you have more stuff there.

Relevant part here (written in a bit more terse javascript):

for (let i = 0; i < data.Contents.length; i++) {
 const key = `https://s3.amazonaws.com/lifestyle345/${ data.Contents[i].Key }`;
  if (key.substring(0, 19) != end) {
     const endObject = {
         slideName: 'Whatever slide. Where do you get the info from?',
         imageUrl: key,
         textUrl: 'whatever, also no info',
     }    
     list.push(endObject );
  } 

}

As you can see, I'm pushing the object itself into the list, not just a string with the url.

Alternatively, you can gater the list as you do, and then at the end, loop it and get your list:

const list = [];
for (...) {
  ...
  list.push(url);
}
// after you get the list of URLs, you get the objects:
const objects  = list.map(function (url) {
  return {
         slideName: 'Whatever slide. Where do you get the info from?',
         imageUrl: url,
         textUrl: 'whatever, also no info',
     }
});

console.log(JSON.stringify(objects, null, 2));

Note 1 : I don't know where you get the textUrl from so I'm skipping it for now. Note 2 : You'd better move the var list = [] statement inside your listAllKeys function, or it could hold all the values you got, from the first time you started calling the function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM