I started programming a year ago and I'm quite new at JavaScript/jQuery languages. I'm currently doing an internship as a web developer, and I have to work a lot with JSON files.
JSON files I work with are presented this way:
{"sessions": [{"waves": [{"moments": [{ "lat": 43.522182, "lon": -1.5024,"type": "Z"},{"lat": 43.522182,"lon": -1.5024,"type": "T"},{"lat": 43.522182,"lon": -1.5024,"type": "R"},{"lat": 43.522182,"lon": -1.5024,"type": "Z"},{"lat": 43.522182, "lon": -1.5024, "type": "Z"},{ "lat": 43.522182,"lon": -1.5024,"type": "R"}, { "lat": 43.522182,"lon": -1.5024,"type": "Z"},{"lat": 43.522182,"lon": -1.5024,"type": "Z"}],"direction": "left","date": "05/03/18","time": "09:46:34","lat": 43.522186,"lon": -1.5024,"level": 0, "distance": 0,"speed": 0.03,"duration": 23.94},{"moments": [{"lat": 43.522182,"lon": -1.5024,"type": "Z"},{"lat": 43.822182,"lon": -4.5024,"type": "T"},{"lat": 43.822182,"lon": -4.5024,"type": "R"},{"lat": 43.522182,"lon": -4.5024,"type": "Z"},{"lat": 43.822182,"lon": -4.5024,"type": "Z"},{"lat": 43.822182,"lon": -4.5024,"type": "R"},{"lat": 43.822182,"lon": -4.5024,"type": "Z"}],"direction": "left","date": "05/03/18","time": "10:32:10","lat": 43.822186, "lon": -4.5024,"level": 2,"distance": 60,"speed": 0.53,"duration": 20.01},{"moments": [{"lat": 43.522182,"lon": -1.5024,"type": "Z"},{"lat": 43.822182,"lon": -4.5024,"type": "T"},{"lat": 43.822182,"lon": -4.5024,"type": "R"},{"lat": 43.522182,"lon": -4.5024,"type": "Z"},{"lat": 43.822182,"lon": -4.5024,"type": "Z"},{"lat": 43.822182,"lon": -4.5024,"type": "R"},{"lat": 43.822182,"lon": -4.5024,"type": "Z"}],"direction": "left","date": "05/03/18","time": "10:32:10","lat": 43.822186, "lon": -4.5024,"level": 6,"distance": 50,"speed": 1.5,"duration": 25}]}],"date": "05/03/18","time": "09:46:34","lat": 43.522197,"lon": -1.502392,"duration": 5425}
Which give us something like this: well structured json file
This json file contains 1 surf session. Each surf session is composed of several waves you took during the session, and each wave is characterized by moments.
One of my internship goal would be to create kind of a history json file in which would be stored every sessions of a person. So each time an user would upload a session json file on the website I work on, the content of that new file would be added at the following of the history file.
Here's an example of the history json file with 2 sessions:
{"sessions":[{"waves":[{"moments":[{"lat":1,"lon":-1,"type":"Z"},{"lat":2,"lon":-2,"type":"T"},{"lat":3,"lon":-3,"type":"R"},{"lat":4,"lon":-4,"type":"Z"},{"lat":5,"lon":-5,"type":"Z"},{"lat":6,"lon":-6,"type":"R"},{"lat":7,"lon":-7,"type":"Z"},{"lat":8,"lon":-8,"type":"Z"}],"direction":"left","date":"05/03/18","time":"09:46:34","lat":43.522186,"lon":-1.5024,"level":0,"distance":0,"speed":2,"duration":23.94},{"moments":[{"lat":9,"lon":-9,"type":"Z"},{"lat":10,"lon":-10,"type":"T"},{"lat":11,"lon":-11,"type":"R"},{"lat":12,"lon":-12,"type":"Z"},{"lat":13,"lon":-13,"type":"Z"},{"lat":14,"lon":-14,"type":"R"},{"lat":15,"lon":-15,"type":"Z"}],"direction":"left","date":"05/03/18","time":"10:32:10","lat":43.822186,"lon":-4.5024,"level":1,"distance":5,"speed":3,"duration":12},{"moments":[{"lat":16,"lon":-16,"type":"Z"},{"lat":17,"lon":-17,"type":"T"},{"lat":18,"lon":-18,"type":"R"},{"lat":19,"lon":-19,"type":"Z"},{"lat":20,"lon":-20,"type":"Z"},{"lat":21,"lon":-21,"type":"R"},{"lat":22,"lon":-22,"type":"Z"}],"direction":"left","date":"05/03/18","time":"10:32:10","lat":43.822186,"lon":-4.5024,"level":6,"distance":10,"speed":1,"duration":50},{"moments":[{"lat":23,"lon":-23,"type":"Z"},{"lat":24,"lon":-24,"type":"T"},{"lat":25,"lon":-25,"type":"R"},{"lat":26,"lon":-26,"type":"Z"},{"lat":27,"lon":-27,"type":"Z"},{"lat":28,"lon":-28,"type":"R"},{"lat":29,"lon":-29,"type":"Z"}],"direction":"left","date":"05/03/18","time":"10:32:10","lat":43.822186,"lon":-4.5024,"level":2,"distance":60,"speed":30,"duration":10},{"moments":[{"lat":30,"lon":-30,"type":"Z"},{"lat":31,"lon":-31,"type":"T"},{"lat":32,"lon":-32,"type":"R"},{"lat":33,"lon":-33,"type":"Z"},{"lat":34,"lon":-34,"type":"Z"},{"lat":35,"lon":-35,"type":"R"},{"lat":36,"lon":-36,"type":"Z"}],"direction":"left","date":"05/03/18","time":"10:32:10","lat":43.822186,"lon":-4.5024,"level":4,"distance":24,"speed":50,"duration":30}]},{"waves":[{"moments":[{"lat":1,"lon":-1,"type":"Z"},{"lat":2,"lon":-2,"type":"T"},{"lat":3,"lon":-3,"type":"R"},{"lat":4,"lon":-4,"type":"Z"},{"lat":5,"lon":-5,"type":"Z"},{"lat":6,"lon":-6,"type":"R"},{"lat":7,"lon":-7,"type":"Z"},{"lat":8,"lon":-8,"type":"Z"}],"direction":"left","date":"05/03/18","time":"09:46:34","lat":43.522186,"lon":-1.5024,"level":0,"distance":0,"speed":2,"duration":23.94},{"moments":[{"lat":9,"lon":-9,"type":"Z"},{"lat":10,"lon":-10,"type":"T"},{"lat":11,"lon":-11,"type":"R"},{"lat":12,"lon":-12,"type":"Z"},{"lat":13,"lon":-13,"type":"Z"},{"lat":14,"lon":-14,"type":"R"},{"lat":15,"lon":-15,"type":"Z"}],"direction":"left","date":"05/03/18","time":"10:32:10","lat":43.822186,"lon":-4.5024,"level":1,"distance":5,"speed":3,"duration":12},{"moments":[{"lat":16,"lon":-16,"type":"Z"},{"lat":17,"lon":-17,"type":"T"},{"lat":18,"lon":-18,"type":"R"},{"lat":19,"lon":-19,"type":"Z"},{"lat":20,"lon":-20,"type":"Z"},{"lat":21,"lon":-21,"type":"R"},{"lat":22,"lon":-22,"type":"Z"}],"direction":"left","date":"05/03/18","time":"10:32:10","lat":43.822186,"lon":-4.5024,"level":6,"distance":10,"speed":1,"duration":50},{"moments":[{"lat":23,"lon":-23,"type":"Z"},{"lat":24,"lon":-24,"type":"T"},{"lat":25,"lon":-25,"type":"R"},{"lat":26,"lon":-26,"type":"Z"},{"lat":27,"lon":-27,"type":"Z"},{"lat":28,"lon":-28,"type":"R"},{"lat":29,"lon":-29,"type":"Z"}],"direction":"left","date":"05/03/18","time":"10:32:10","lat":43.822186,"lon":-4.5024,"level":2,"distance":60,"speed":30,"duration":10},{"moments":[{"lat":30,"lon":-30,"type":"Z"},{"lat":31,"lon":-31,"type":"T"},{"lat":32,"lon":-32,"type":"R"},{"lat":33,"lon":-33,"type":"Z"},{"lat":34,"lon":-34,"type":"Z"},{"lat":35,"lon":-35,"type":"R"},{"lat":36,"lon":-36,"type":"Z"}],"direction":"left","date":"05/03/18","time":"10:32:10","lat":43.822186,"lon":-4.5024,"level":4,"distance":24,"speed":50,"duration":30}]}],"date":"05/03/18","time":"09:46:34","lat":43.522197,"lon":-1.502392,"duration":3012}
Which would give us this: Expected history json file
I've been trying a lot of things for a few days, especially turning my objects into arrays, then using the push method and then return the arrays into objects again, the closest point I got is this:
function toObject(arr) { //Allows to turn an array into an object
var rv = {};
for (var i = 0; i < arr.length; ++i)
rv[i] = arr[i];
return rv;
}
Object.size = function(obj) { //Return the size of the object
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};
var array=[];
//obj is my new session I want to add in obj2
var obj = {"sessions": [{"waves": [{"moments": [{ "lat": 43.522182, "lon": -1.5024,"type": "Z"},{"lat": 43.522182,"lon": -1.5024,"type": "T"},{"lat": 43.522182,"lon": -1.5024,"type": "R"},{"lat": 43.522182,"lon": -1.5024,"type": "Z"},{"lat": 43.522182, "lon": -1.5024, "type": "Z"},{ "lat": 43.522182,"lon": -1.5024,"type": "R"}, { "lat": 43.522182,"lon": -1.5024,"type": "Z"},{"lat": 43.522182,"lon": -1.5024,"type": "Z"}],"direction": "left","date": "05/03/18","time": "09:46:34","lat": 43.522186,"lon": -1.5024,"level": 0, "distance": 0,"speed": 0.03,"duration": 23.94},{"moments": [{"lat": 43.522182,"lon": -1.5024,"type": "Z"},{"lat": 43.822182,"lon": -4.5024,"type": "T"},{"lat": 43.822182,"lon": -4.5024,"type": "R"},{"lat": 43.522182,"lon": -4.5024,"type": "Z"},{"lat": 43.822182,"lon": -4.5024,"type": "Z"},{"lat": 43.822182,"lon": -4.5024,"type": "R"},{"lat": 43.822182,"lon": -4.5024,"type": "Z"}],"direction": "left","date": "05/03/18","time": "10:32:10","lat": 43.822186, "lon": -4.5024,"level": 2,"distance": 60,"speed": 0.53,"duration": 20.01},{"moments": [{"lat": 43.522182,"lon": -1.5024,"type": "Z"},{"lat": 43.822182,"lon": -4.5024,"type": "T"},{"lat": 43.822182,"lon": -4.5024,"type": "R"},{"lat": 43.522182,"lon": -4.5024,"type": "Z"},{"lat": 43.822182,"lon": -4.5024,"type": "Z"},{"lat": 43.822182,"lon": -4.5024,"type": "R"},{"lat": 43.822182,"lon": -4.5024,"type": "Z"}],"direction": "left","date": "05/03/18","time": "10:32:10","lat": 43.822186, "lon": -4.5024,"level": 6,"distance": 50,"speed": 1.5,"duration": 25}]}],"date": "05/03/18","time": "09:46:34","lat": 43.522197,"lon": -1.502392,"duration": 5425};
array.push(obj);
myJSON = toObject(array);
var myJSON = JSON.stringify(myJSON);
document.getElementById("demo").innerHTML = myJSON;
//obj 2 is my history file that already contains 1 session
var obj2 = {"sessions":[{"waves":[{"moments":[{"lat":1,"lon":-1,"type":"Z"},{"lat":2,"lon":-2,"type":"T"},{"lat":3,"lon":-3,"type":"R"},{"lat":4,"lon":-4,"type":"Z"},{"lat":5,"lon":-5,"type":"Z"},{"lat":6,"lon":-6,"type":"R"},{"lat":7,"lon":-7,"type":"Z"},{"lat":8,"lon":-8,"type":"Z"}],"direction":"left","date":"05/03/18","time":"09:46:34","lat":43.522186,"lon":-1.5024,"level":0,"distance":0,"speed":2,"duration":23.94},{"moments":[{"lat":9,"lon":-9,"type":"Z"},{"lat":10,"lon":-10,"type":"T"},{"lat":11,"lon":-11,"type":"R"},{"lat":12,"lon":-12,"type":"Z"},{"lat":13,"lon":-13,"type":"Z"},{"lat":14,"lon":-14,"type":"R"},{"lat":15,"lon":-15,"type":"Z"}],"direction":"left","date":"05/03/18","time":"10:32:10","lat":43.822186,"lon":-4.5024,"level":1,"distance":5,"speed":3,"duration":12},{"moments":[{"lat":16,"lon":-16,"type":"Z"},{"lat":17,"lon":-17,"type":"T"},{"lat":18,"lon":-18,"type":"R"},{"lat":19,"lon":-19,"type":"Z"},{"lat":20,"lon":-20,"type":"Z"},{"lat":21,"lon":-21,"type":"R"},{"lat":22,"lon":-22,"type":"Z"}],"direction":"left","date":"05/03/18","time":"10:32:10","lat":43.822186,"lon":-4.5024,"level":6,"distance":10,"speed":1,"duration":50},{"moments":[{"lat":23,"lon":-23,"type":"Z"},{"lat":24,"lon":-24,"type":"T"},{"lat":25,"lon":-25,"type":"R"},{"lat":26,"lon":-26,"type":"Z"},{"lat":27,"lon":-27,"type":"Z"},{"lat":28,"lon":-28,"type":"R"},{"lat":29,"lon":-29,"type":"Z"}],"direction":"left","date":"05/03/18","time":"10:32:10","lat":43.822186,"lon":-4.5024,"level":2,"distance":60,"speed":30,"duration":10},{"moments":[{"lat":30,"lon":-30,"type":"Z"},{"lat":31,"lon":-31,"type":"T"},{"lat":32,"lon":-32,"type":"R"},{"lat":33,"lon":-33,"type":"Z"},{"lat":34,"lon":-34,"type":"Z"},{"lat":35,"lon":-35,"type":"R"},{"lat":36,"lon":-36,"type":"Z"}],"direction":"left","date":"05/03/18","time":"10:32:10","lat":43.822186,"lon":-4.5024,"level":4,"distance":24,"speed":50,"duration":30}]}],"date":"05/03/18","time":"09:46:34","lat":43.522197,"lon":-1.502392,"duration":3012};
var size = Object.size(obj2.sessions);
console.log(size);
array.push(obj2);
myJSON = toObject(array);
myJSON = JSON.stringify(myJSON);
document.getElementById("demo2").innerHTML = myJSON;
<!DOCTYPE html>
<html>
<body>
<h2>Create JSON string from a JavaScript object.</h2>
<p id="demo"></p>
<p id="demo2"></p>
</body>
</html>
But it gives me this : My code result
So does someone know how to get the expected history json file? (image 2) Feel free to tell me if my explanations are not well enough to understand what I look for. Thank you