How to represent an Object's key, which has an array of objects as value, in csv

scenario

I have an object like this [see below] and I want to convert this into a csv file. The comments array could have n no of comments, maybe 100, maybe 500, maybe none. I understand representing key's with a string value in csv, but how do I go about representing the VIDEO_COMMENTS part, which itself is an array of objects, in csv?

{
  "VIDEO_TITLE": "Seenotretter auf dem Mittelmeer: Gemeinsame Pressekonferenz der deutschen NGOs",
  "VIDEO_VIEWS": "12,385 views",
  "VIDEO_LIKE": "416",
  "VIDEO_DISLIKE": "396",
  "VIDEO_OWNER": "Tilo Jung",
  "VIDEO_PUBLISH_DATE": "Published on Jun 28, 2018",
  "VIDEO_URL": "https://www.youtube.com/watch?v=gWI8Ja7X8gg",
  "VIDEO_COMMENTS": [
    {
      "comment_by": "DunklerZebralord",
      "comment_text": "Eine Träne für Tilo, wenn er diese Kommentarspalte aufmacht... This shit is rough.",
      "comment_time": "1 week ago",
      "comment_like": 51
    },
    {
      "comment_by": "Gruppe42",
      "comment_text": "So recht gut scheint die Mobilisierung der \"Reconquista Teutonia\" nicht mehr zu laufen. Gerade mal 275 Dislikes. Wenn man bedenkt, dass mindestens drei immer von einem Bübchen kommen dann wirkt es noch armseliger.",
      "comment_time": "1 week ago",
      "comment_like": 6
    },
    {
      "comment_by": "Keysa Button",
      "comment_text": "Das sind also diese richtig guten Menschen?",
      "comment_time": "1 week ago",
      "comment_like": 70
    }]
}

Update

This is a representation I found from https://json-csv.com/, and this looks clean, gives the follwing ind of csv for the above json

"VIDEO_TITLE","VIDEO_VIEWS","VIDEO_LIKE","VIDEO_DISLIKE","VIDEO_OWNER","VIDEO_PUBLISH_DATE","VIDEO_URL","VIDEO_COMMENTS__comment_by","VIDEO_COMMENTS__comment_text","VIDEO_COMMENTS__comment_time","VIDEO_COMMENTS__comment_like"
"Seenotretter auf dem Mittelmeer: Gemeinsame Pressekonferenz der deutschen NGOs","12,385 views","416","396","Tilo Jung","Published on Jun 28, 2018","https://www.youtube.com/watch?v=gWI8Ja7X8gg","DunklerZebralord","Eine Träne für Tilo, wenn er diese Kommentarspalte aufmacht... This shit is rough.","1 week ago","51"
"","","","","","","","Gruppe42","So recht gut scheint die Mobilisierung der ""Reconquista Teutonia"" nicht mehr zu laufen. Gerade mal 275 Dislikes. Wenn man bedenkt, dass mindestens drei immer von einem Bübchen kommen dann wirkt es noch armseliger.","1 week ago","6"

can anybody guide in how to achieve the same in nodejs?

1 answer

  • answered 2018-07-11 06:50 Vasu

    In your scenario a single video is mapped to many comments (one-to-many). So a single video object can't be put into a single csv line, unless all the comments are somehow appended to each video line. So csv line in this case can be

    video1_title, video1_url, ...., comment11_text, comment11_time,..., comment12_text

    video2_title, video2_url......, comment21_text, comment21_time

    See the code for that.

    var csvline = [];
    
    // video  is your object loop over its values
    
    Object.values(video).forEach(attrb => { 
    
    // if attribute is comments array loop over it
    
        if(Array.isArray(attrb)){           
            attrb.forEach(comments => {
    
                // loop over comments 
                Object.values(comments).forEach(c_attrb => {
                    csvline.push(c_attrb);
                })
            })
        } else {
            csvline.push(attrb);
        }
    })
    
    console.log(csvline);  // see the result