Alucard
Alucard

Reputation: 103

Typescript model class for JSON object

I have the following JSON:

{
"-KtDGS8GdOcJ33Lcqjok": {
    "2017": {
        "address": "test address 1",
        "area2": "3212",
        "author": "STOkzlbT4OeOcbO2ed9Z7dvxZk92",
        "category": "Solar",
        "client": "Test Contact",
        "createdAt": 1504551753483,
        "lastEdited": 1504551805648,
        "lastEditedBy": "STOkzlbT4OeOcbO2ed9Z7dvxZk92",
        "lat": "18.490362758827665",
        "lng": "-69.93279173970221",
        "name": "17002 - test",
        "pictures": {
            "a95ff256-0f05-3122-a6b5-a88d3fd14c3f": true
        },
        "price": "213",
        "province": "-KtDBavhyLhrpV8hDuj2",
        "sector": "-KtDBqgy3CqpTv6c_iQ9",
        "totalPrice": "1234",
        "year": "2017"
    }
},
"-KtDGaU9BB6eNj-MsyBg": {
    "2015": {
        "address": "test 2a",
        "area1": "3245",
        "author": "STOkzlbT4OeOcbO2ed9Z7dvxZk92",
        "category": "Local: comercio",
        "client": "test2 ",
        "createdAt": 1504552100747,
        "lastEdited": 1504552100747,
        "lastEditedBy": "STOkzlbT4OeOcbO2ed9Z7dvxZk92",
        "lat": "18.489417118875462",
        "level": "4",
        "lng": "-69.92930956184864",
        "name": "15002 - test2a ",
        "parking": "12",
        "plaza": "Agora",
        "price": "213",
        "province": "-KtDBavhyLhrpV8hDuj2",
        "restrooms": "2",
        "sector": "-KtDBqgy3CqpTv6c_iQ9",
        "totalPrice": "213",
        "year": "2015"
    },
    "2017": {
        "address": "test 2",
        "area1": "3245",
        "author": "STOkzlbT4OeOcbO2ed9Z7dvxZk92",
        "category": "Local: comercio",
        "client": "test2 ",
        "createdAt": 1504551790632,
        "lastEdited": 1504551790632,
        "lastEditedBy": "STOkzlbT4OeOcbO2ed9Z7dvxZk92",
        "lat": "18.489417118875462",
        "level": "4",
        "lng": "-69.92930956184864",
        "name": "17003 - test2b",
        "parking": "12",
        "plaza": "Agora",
        "price": "213",
        "province": "-KtDBavhyLhrpV8hDuj2",
        "restrooms": "2",
        "sector": "-KtDBqgy3CqpTv6c_iQ9",
        "totalPrice": "213",
        "year": "2017"
    }
},
"codeCounter": {
    "2015": 2,
    "2017": 5
},
"totals": {
    "2015": 1,
    "2017": 5
} 
}

It's basically a list of objects, each object contains one or more nested objects wrapped in the year they were made (physically these are houses,apartments, etc.).

Where I'm having trouble is trying to map the 'year' objects (2017, 2015, etc') as they may or may not exist. For example, an object may have both 2017, 2016 entries or only one of them, etc.

I already have a 'Property' model class that I believe works, it has all the address, author, category, etc. fields. I'm trying to create the outter class that would contain a list of these property objects:

export interface PropertyWrapper {
  [year: number]: Property;
}

I tried parsing the JSON as a PropertyWrapper[] array and this way I can already access a property by calling:

for (const pw of data) {
        console.log(pw[2017]);
      }

But this only works because I already know the object has a '2017' entry. How could I do this dynamically regardless of whether or not there's a '2017', '2010', or 10 entries ?

Upvotes: 0

Views: 103

Answers (1)

JSingh
JSingh

Reputation: 375

Are you looking for something like this. First gets object keys, then loop through these keys and then loop through nested object keys

var jso = {
"-KtDGS8GdOcJ33Lcqjok": {
    "2017": {
        "address": "test address 1",
        "area2": "3212",
        "author": "STOkzlbT4OeOcbO2ed9Z7dvxZk92",
        "category": "Solar",
        "client": "Test Contact",
        "createdAt": 1504551753483,
        "lastEdited": 1504551805648,
        "lastEditedBy": "STOkzlbT4OeOcbO2ed9Z7dvxZk92",
        "lat": "18.490362758827665",
        "lng": "-69.93279173970221",
        "name": "17002 - test",
        "pictures": {
            "a95ff256-0f05-3122-a6b5-a88d3fd14c3f": true
        },
        "price": "213",
        "province": "-KtDBavhyLhrpV8hDuj2",
        "sector": "-KtDBqgy3CqpTv6c_iQ9",
        "totalPrice": "1234",
        "year": "2017"
    }
},
"-KtDGaU9BB6eNj-MsyBg": {
    "2015": {
        "address": "test 2a",
        "area1": "3245",
        "author": "STOkzlbT4OeOcbO2ed9Z7dvxZk92",
        "category": "Local: comercio",
        "client": "test2 ",
        "createdAt": 1504552100747,
        "lastEdited": 1504552100747,
        "lastEditedBy": "STOkzlbT4OeOcbO2ed9Z7dvxZk92",
        "lat": "18.489417118875462",
        "level": "4",
        "lng": "-69.92930956184864",
        "name": "15002 - test2a ",
        "parking": "12",
        "plaza": "Agora",
        "price": "213",
        "province": "-KtDBavhyLhrpV8hDuj2",
        "restrooms": "2",
        "sector": "-KtDBqgy3CqpTv6c_iQ9",
        "totalPrice": "213",
        "year": "2015"
    },
    "2017": {
        "address": "test 2",
        "area1": "3245",
        "author": "STOkzlbT4OeOcbO2ed9Z7dvxZk92",
        "category": "Local: comercio",
        "client": "test2 ",
        "createdAt": 1504551790632,
        "lastEdited": 1504551790632,
        "lastEditedBy": "STOkzlbT4OeOcbO2ed9Z7dvxZk92",
        "lat": "18.489417118875462",
        "level": "4",
        "lng": "-69.92930956184864",
        "name": "17003 - test2b",
        "parking": "12",
        "plaza": "Agora",
        "price": "213",
        "province": "-KtDBavhyLhrpV8hDuj2",
        "restrooms": "2",
        "sector": "-KtDBqgy3CqpTv6c_iQ9",
        "totalPrice": "213",
        "year": "2017"
    }
},
"codeCounter": {
    "2015": 2,
    "2017": 5
},
"totals": {
    "2015": 1,
    "2017": 5
} 
};

Object.keys(jso).forEach(function(key) {
	Object.keys(jso[key]).forEach(function(nestedKey){
  	console.log(`nestedKey: ${nestedKey} \n ` , jso[key][nestedKey]);
  });
});

Upvotes: 1

Related Questions