0

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

1
  • 3
    you can use JSON.parse to get actual object and then extract data from object by using for in loop Commented Nov 13, 2017 at 11:32

2 Answers 2

2
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.

Sign up to request clarification or add additional context in comments.

5 Comments

I'm getting an error with this code. data.forEach(function(event)){ SyntaxError: Unexpected token ) var data = JSON.parse(data); data.forEach(function(event)){ var id = events.id; var venue = events.place.name; var address = events.location.street + " " + events.location.city " " + events.location.zip; var coverPicture = events.coverPicture; var description = events.description; var startTime = events.startTime; var endTime = events.endTime; //so on...and create new objects here. }
I have var data = JSON.parse("data"); Then I changed it to data.events.forEach(function(event)){.... but I still get the same error - unexpected token..?
what is the json data that you are getting? Can you paste it here?
{ "events": [ { "id": "18110, "name": "Lime of your Life", "type": "public", "coverPicture": "scontent.xx.fbcdn.net/n.jpg?oh=b", "profilePicture": "scontent.xx.fbcdn.net/sss.jpg?oh=32", "description": "desc!", "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": "redbrickbuilding.co.uk" },
Here events is an array rite. so var data = JSON.parse("your data") data.events.forEach( "your code")
1

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)

Comments

Your Answer

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.