4

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

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
noyttra
  • 53
  • 3
  • A simple search for merging arrays should produce lots of results. Suggest you spend some time going through the [MDN Array Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) and familiarize yourself with all the various built in methods from menu on left. Lots of array tutorials on web also – charlietfl May 30 '18 at 12:42
  • Do same for Object. For example your `size` approach can be done using `var size = Object.keys(obj).length`. – charlietfl May 30 '18 at 12:44
  • In my opinion If you really want to understand JSONs standard. You should read the specification [RFC-7159](https://tools.ietf.org/html/rfc7159). Also i suggest you look into the ideology of RESTful Services, not so much about the structure but about the ideas behind JSON in general. – KALALEX May 30 '18 at 12:48
  • @charlietfl Thanks for your comments but what do you mean exactly by "Do the same for Object"? The code you wrote will return the length of "session" am I right ? – noyttra May 30 '18 at 12:58
  • @KALALEX Thanks i'll check it out – noyttra May 30 '18 at 12:58
  • @KALALEX OP is not asking about JSON itself but how to work with it. – charlietfl May 30 '18 at 13:02
  • By *"same with Object"* I mean go through all the various built in methods in same MDN docs – charlietfl May 30 '18 at 13:04
  • @charlietfl Using something without studing, understanding and learning it leads to sciolism which is the worst state you could be in as programmer. Starting to program with shiny new tech without having the knowledge only leads to bad programming and problems. – KALALEX May 30 '18 at 13:15
  • @KALALEX sure but sending someone learning how to work with arrays and objects in javascript to read JSON spec is a complete side track and certainly won't help them with the immediate problem at hand. This question is not about JSON – charlietfl May 30 '18 at 13:18

2 Answers2

1

You will have to change the code to following

Size.forEach(function(I,index){

Obj.sessions.push(index);}

Right now you are trying to merge the 2 session array with main array

By using array.push so you are adding old session with new one it will reflect both sessions as in the image file shown

Correct answer to reflect image

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};

//clear array since array already contains the obj 
var array = [] ;
var size = Object.size(obj2.sessions);
console.log(size);

//now add the waves to obj2 in order to format as required
obj.sessions.forEach(function(index,object){ 
obj2.sessions.push(object);
}

array.push(obj2);
myJSON = toObject(array);
myJSON = JSON.stringify(myJSON);
document.getElementById("demo2").innerHTML = myJSON;

Adding plunker link https://plnkr.co/edit/UNWbyDlifsJepNXXcQMI

Shyam Joshi
  • 387
  • 4
  • 9
  • Thanks for your answer @user655685, do you mean that I should change my array.push(obj) and array.push(obj2) and replace it with what you wrote ? Sorry, I'm not well experienced so I don't know where to place it. – noyttra May 30 '18 at 13:01
  • Your solution's almost what I'm looking for, I copied/pasted the result into an online json editor and the result is this : [link](https://jsoneditoronline.org/?id=e1b433c14c1743bbacdfd41e9304abbc) So the sessions array is composed of two elements, when you open sessions[0] you find the new array "waves" (that's how it should look), but then when you open sessions[1] you don't find directly the array "waves" but you have to open another "sessions" array. Is there a way to have directly the "waves" array right after opening sessions[1] ? – noyttra May 30 '18 at 14:43
  • obj.sessions.forEach(function(index,object){ obj2.sessions.push(object); }); Instread obj2.sessions.push(index); it will solve the problem – Shyam Joshi May 30 '18 at 14:50
  • Okay thank you very much that's great !! I imagine that in any case, sessions array will have to be nested inside the "0" below and can't be directly below "object" ? [history file](https://jsoneditoronline.org/?id=e1b433c14c1743bbacdfd41e9304abbc) – noyttra May 30 '18 at 14:59
  • which object are you looking at my plunker is producing the exact result , in the demo2 the json is coming right https://plnkr.co/edit/UNWbyDlifsJepNXXcQMI make sure you copy from demo2 only because you are putting both out this could be causing problems. and if you find this answer working mark and close the post thank you – Shyam Joshi May 30 '18 at 15:06
  • I said this because looking at the result on the json online editor, I had to open"object", then open"0" and then i found sessions, and I thought I had to write something like `obj[0].sessions` to access the sessions array, but writing `obj.sessions` is fine, so I guess you solved my problem. Thank you very much again ! – noyttra May 30 '18 at 15:24
0

If all you want is to merge the sessions arrays into main history this is really quite a trivial operation using Array#push()

masterObject.sessions.push(newObj.sessions)
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • Yes but I can't use this if masterObject is an Object and not an array. And the thing is I don't want to loose the "hierarchy" of the json. In the third picture I posted we can see that the very 2 first elements are 0 and 1, and inside of each there is a session. What is expected is that 'sessions' would be the very first element of the json file – noyttra May 30 '18 at 13:19
  • The `sessions` property of each object is array. Go though this also [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json). That will give you a better comprehension of what you are actually working with – charlietfl May 30 '18 at 13:21
  • I think your main problem is you need to learn to be able to read the general structure – charlietfl May 30 '18 at 13:24