I've a JSON file with a structure that is not yet set; it may grow complex. I want to keep track of what the functions think the data structure is.
What are standard/smart ways to show that your functions is connected to a certain data structure?
Right now, I'm using a _comment in the json file to keep a version number, and then keeping that version name as a comment in each function that uses it. Full example below.
travel.json
{
"_comment" : "version 1.0"
, "name" : "Tom Sawyer"
, "travel" : [{
"id" : "1"
, "location" : "San Francisco"
}, {
"id" : "2"
, "location" : "London"
}]
}
Two functions to parse: travel.json
fs = require('fs');
function get_json() {
var file = __dirname + '/travel.json';
data = fs.readFileSync(file, 'utf8');
var json_obj = JSON.parse(data);
return(json_obj);
};
function get_location(json_obj) {
// "version 1.0"
var new_obj = {};
json_obj.content.forEach(function(item) {
new_obj[item.id] = item.location;
});
return ( new_obj );
};
// Run
console.log('Locations: ', get_location( get_json() ));
Thanks.
It looks a bit like defensive programming. If you are creating an API that other people will depend on you could version the url of the API instead of the JSON. Something like yoururl/api/1/locations, yoururl/api/2/locations, etc