0

I want to turn this JSON file into arrays:

{"Elements":[{"LowerTolerance":1.4,"Name":"abc","ReferenceValue":1.5,"UpperTolerance":1.6,"Valid":false,"Value":1.8},{"LowerTolerance":20,"Name":"def","ReferenceValue":21.5,"UpperTolerance":23,"Valid":true,"Value":22.8},{"LowerTolerance":4.5,"Name":"ghi","ReferenceValue":5,"UpperTolerance":5.5,"Valid":false,"Value":4}],"Kamera":"c1"}

Here is a picture of the JSON file in a ordered tree form: JSON file

I want to get arrays like lowertolerance[], name[], referencevalue[] etc. so when I call an element of the array I get the value of it. For example: name[2] = ghi or referencevalue[0] = 1.5

I found this: https://stackoverflow.com/questions/6857468/converting-a-js-object-to-an-array#=

Now I have the problem that I don't know what to do when you have an object inside an object like I do.

I tried this:

var o = {"Elements": [{"LowerTolerance": 1.4, "Name": "abc", "ReferenceValue": 1.5, "UpperTolerance": 1.6}, {"LowerTolerance": 1.4, "Name": "abc", "ReferenceValue": 1.5, "UpperTolerance": 1.6}, {"LowerTolerance": 1.4, "Name": "abc", "ReferenceValue": 1.5, "UpperTolerance": 1.6}], "Kamera": "c1"};

var arr = $.map(o, function(el) { return el; })

document.getElementById("output").innerHTML = arr;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="output">
</div>

(jQuery is needed)

The ouput is: "[object Object],[object Object],[object Object],c1". What do I have to do to get the objects inside the main object?

You would make me really happy if you could help me!


Here is a simpler example of my problem:

 var myObj = [{1:1, 2:2, 3:3}, {4:4, 5:5, 6:6}];

 var array = $.map(myObj, function(value, index) {
  return [value];
 });
 document.getElementById("output").innerHTML = array;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="output"></p>
Joh
  • 327
  • 4
  • 13

2 Answers2

0

Your JSON object:

myJson = {"Elements":[{"LowerTolerance":1.4,"Name":"abc","ReferenceValue":1.5,"UpperTolerance":1.6,"Valid":false,"Value":1.8},{"LowerTolerance":20,"Name":"def","ReferenceValue":21.5,"UpperTolerance":23,"Valid":true,"Value":22.8},{"LowerTolerance":4.5,"Name":"ghi","ReferenceValue":5,"UpperTolerance":5.5,"Valid":false,"Value":4}],"Kamera":"c1"}

Create each array, for example:

var lowerToleranceArray = [];

Now, loop through your JSON and push each value to the array:

for (var i=0; i<myJson.Elements.length; i++) {
lowerToleranceArray.push(myJson.Elements[i].LowerTolerance);
}

Now, you have your lowerTolerance array. The same thing for the other arrays (push in the same loop).

treecon
  • 2,415
  • 2
  • 14
  • 28
0

Using ES6 for...of loop :

var jsonObj = {
 "Elements": [{
  "LowerTolerance": 1.4,
  "Name": "abc",
  "ReferenceValue": 1.5,
  "UpperTolerance": 1.6,
  "Valid": false,
  "Value": 1.8
 }, {
  "LowerTolerance": 20,
  "Name": "def",
  "ReferenceValue": 21.5,
  "UpperTolerance": 23,
  "Valid": true,
  "Value": 22.8
 }, {
  "LowerTolerance": 4.5,
  "Name": "ghi",
  "ReferenceValue": 5,
  "UpperTolerance": 5.5,
  "Valid": false,
  "Value": 4
 }],
 "Kamera": "c1"
};

let lowertolerance = [];
let name = [];
let referencevalue = [];
let uppertolerance = [];
let valid = [];
let value = [];

for (let item of jsonObj.Elements) {
  lowertolerance.push(item.LowerTolerance);
  name.push(item.Name);
  referencevalue.push(item.ReferenceValue);
  uppertolerance.push(item.UpperTolerance);
  valid.push(item.Valid);
  value.push(item.Value);
}

console.log("lowertolerance :", lowertolerance);
console.log("name :", name);
console.log("referencevalue :", referencevalue);
console.log("uppertolerance :", uppertolerance);
console.log("valid :", valid);
console.log("value :", value);
Debug Diva
  • 26,058
  • 13
  • 70
  • 123