Bwizard
Bwizard

Reputation: 1023

How to create an object from a JSON file in Node.js

I have dowloaded a json file which contains a lot of information that I do not need for my end purpose of rendering a table to a html page containing just some of the items of the data.

the data.json looks like

{ "events": [
 {
  "id": "181101322507029",
  "name": "Carnival Special",
  "type": "public",
  "coverPicture": "https://scontent.xx.fbcdn.net/v/t1.0.jpg?oh=b9eC",
  "profilePicture": "https://scontent.xx.fbcdn.net/v/t1.0-jpg?oh=32c4574",
  "description": "Lots and lots of info blah blah",
  "distance": "1546",
  "startTime": "2017-11-18T22:00:00+0000",
  "endTime": "2017-11-19T03:00:00+0000",
  "timeFromNow": 476169,
  "isCancelled": false,
  "isDraft": false,
  "category": null,
  "ticketing": {
    "ticket_uri": "https://nicevenue.co.uk/event/lime-of-your-life/"
  },
  "place": {
    "id": "517137361697082",
    "name": "Nice Venue",
    "location": {
      "city": "Nice City",
      "country": "United Kingdom",
      "latitude": 66.3445334,
      "longitude": -4.7356667928,
      "street": "The Blah Blah, Blah, Blah Road",
      "zip": "AB1 2CD"
    }
  },
  "stats": {
    "attending": 68,
    "declined": 0,
    "maybe": 177,
    "noreply": 336
  },
  "venue": {
    "id": "517137361697082",
    "name": "Nice Venue",
    "about": "Situated in the blah blah aims to provide a friendly atmosphere, gourmet food and fine cocktails.",
    "emails": [
      "[email protected]"
    ],
    "coverPicture": "https://scontent.xx.fbcdn.net/v/t31.0-8/s78_o.jpg?oh=8f62cb6CB2",
    "profilePicture": "https://scontent.xx.fbcdn.net/v/t1.0-1/c0.16n.jpg?oh=8ef5df00f31&oe=5AAF3935",
    "category": "Bar",
    "categoryList": [
      "Bar",
      "Restaurant"
    ],
    "location": {
      "city": "Nice City",
      "country": "United Kingdom",
      "latitude": 65.138604292331,
      "longitude": -9.73566d867928,
      "street": "The Blah Blah, Blah, Blah Road",
      "zip": "AB1 2CD"
    }
  }
},
{
  "id": "1896504087342309",
  "name": "Mr Tea  + Dj Dr SJ",
  "type": "public",events
"
"
etc....

I need to iterate through this data and create a new object (or array) like

{"id": 123456 [    
 {
  "StartTime": "2017-11-18T22:00:00+0000",
  "description": "Lots and lots of info blah blah",
  "place": {
     "id": "517137361697082",
     "name": "Nice Venue",
     "location": {
     "city": "Nice City",
    "country": "United Kingdom",
    "latitude": 66.3445334,
    "longitude": -4.7356667928,
    "street": "The Blah Blah, Blah, Blah Road",
    "zip": "AB1 2CD" 
 }]
 }

I would then like to iterate through this object using an express template to render the info to a table tag in the html.

I have looked at For in loops with hasProperty or by using low-dash _.Map but I don't really understand if I am doing this correctly or even going about what I am trying to do in the best way. Thanks

Upvotes: 0

Views: 81

Answers (2)

ian1095
ian1095

Reputation: 579

If you are using ES6 or above you can do something like that to create a new array with the new object:

If you aren't using ES6, the lodash will have the _map

I will keep it simple by using this example of converting firstname to name:

var test = {
    "students": [
        {
            "firstname": "ian",
            "age": 25
        },
         {
            "firstname": "Rick",
            "age": 25
        }
    ]
}


var newList = test.students.map(data => ({
  name: data.firstname ,
  age: data.age
}));

console.log(newList)

Upvotes: 1

sridhar
sridhar

Reputation: 622

var resObj = JSON.parse("your json data");

resObj.forEach(function(event)){
    var id = event.id;
    var place = event.place;
    //so on...and create new objects here.
}

You can then load the data in the html or make a html string out of this looped and append it to your main div.

Upvotes: 2

Related Questions