Filter a JSON Data


#1

Hi,
I googled the problem, but I did not find a solution.

I have this JSON

{
      "DTSTART;VALUE=DATE":"20180110",
      "DTEND;VALUE=DATE":"20180111",
      "DTSTAMP":"20180323T121234Z",
      "UID":"ed066a7b55192cba78d439460b3b0082",
      "CREATED":"20180101T090000Z",
      "DESCRIPTION":"Gelber Sack nicht vergessen!",
      "LAST-MODIFIED":"20180101T090000Z",
      "LOCATION":"Gutach",
      "SEQUENCE":"0",
      "STATUS":"CONFIRMED",
      "SUMMARY":"Gelber Sack",
      "TRANSP":"TRANSPARENT"
   },
   {
      "DTSTART;VALUE=DATE":"20180110",
      "DTEND;VALUE=DATE":"20180111",
      "DTSTAMP":"20180323T121234Z",
      "UID":"695cd9bd5335d9f6f432846bd0026c96",
      "CREATED":"20180101T090000Z",
      "DESCRIPTION":"Graue Tonne nicht vergessen!",
      "LAST-MODIFIED":"20180101T090000Z",
      "LOCATION":"Gutach",
      "SEQUENCE":"0",
      "STATUS":"CONFIRMED",
      "SUMMARY":"Graue Tonne",
      "TRANSP":"TRANSPARENT"
   },

I want to transform the velue of DTEND into an normal Date object. And I want to print only the datas with date in the future.

Thats my Code

getDatas(){
    this.restProvider.getJson("gutach")
    .subscribe(data=> 
      {
        this.datas = data;

    });
  }

I have no idea… :frowning:


#2

Isn’t this a iCalendar data set? There are probably libraries out there to parse those.

PS: Gutach im Elztal?


#3

try:

return data.filter((item)=>{return item.DTEND > 20180110});

Sorry not sure what date field you need


#4

Yes, it is an ical file. I searched for a library but found none. I am not really happy with this solution either.

It is also my first app.

It is Gutach im Elztal :slight_smile: . I come from the neighboring village.


#5

Unfortunately, I do not get a filter option. Maybe I’m missing the right import?

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { RestProvider } from '../../providers/rest/rest';

In RestProvider I also get no filter options.

Screenshot


#6

The example I provided should work without any extra imports (RestProvider) not required.

This is how I get json data from a file within the assets folder.

buildData(){
          var xx = this;
          let promise = new Promise((resolve, reject) => {
               this.http.get('assets/data/vocab.json').map(res => res.json()).subscribe(data => {
                    xx.mainData = data;
                    xx.getotherdata();
                    resolve();
               })
          });
          return promise;
     }

I think the code your are using the data returned might be an object - just do a console.log(to view it). I have a hunch it might this.datas = data.data

getDatas(){
this.restProvider.getJson(“gutach”)
.subscribe(data=>
{
this.datas = data;

});

}


#7

The code posted above had like 4 anti-patterns in it. Do not attempt anything inside it.

Put a break point in your subscribe and see what data is. If it’s json you need json.parse to turn it from a string into an array (although the"json" you posted looks invalid because there are no array brackets around it.


#8

I’m assuming your date strings there are year month day, I could be wrong. Also you could still get timezone issues from this, it may be worth it for you to use a date library. However, you possibly want something like this: https://jsfiddle.net/po8a111o/11/

This also shows parsing the json into a javascript array if you need it.

EDIT: If you can’t figure it out from this you need to post your restProvider.getJson method, because as it stands there is no way anyone could ever know what data you are actually working with