Bryce Bryce - 4 months ago 4
JSON Question

Looping through .json object

I have a .JSON file full of data similar of this:

[
{
"scan_type": "nexpose",
"date_time": "2016-07-18 18:21:44",
"source_address": "1111",
"source_lat": "22.555260",
"source_lng": "-15.084420",
},
...
]


And I wrote a script that will convert this data to an object, here is my script:

let obj = require('./dataOut.json');
var fs = require('fs');

//json variables
let scan_type = obj[0].scan_type;
let date_time = obj[0].date_time;
let source_address = obj[0].source_address;
let source_lat = obj[0].source_lat;
let source_lng = obj[0].source_lng;

//source contains the new object
var source = {
id: source_lat + source_lng,
"source-lat": source_lat,
"source-lng": source_lng,
"source_address": source_address,
x: {
valueOf: function () {
var latlng = [
source_lat,
source_lng
];
var xy = map.FUNCTION_FOR_CONVERTING_LAT_LNG_TO_X_Y(latlng);
return xy[0];
}
},
y: {
valueOf: function () {
var latlng = [
source_lat,
source_lng
];
var xy = map.FUNCTION_FOR_CONVERTING_LAT_LNG_TO_X_Y(latlng);
return xy[1];
}
}
}
//write out to file and stringify
fs.writeFile('./parentSource.json', JSON.stringify(source, null, 2), 'utf-8');


As you can see, currently I have everything set to the first element [0] from my data file for testing. But I need to change that to where it will loop through each element.

Normally I'd just use a foreach loop, but that has been unsuccessful.

How can I add a loop that will go through each .JSON object and add the values to my script, so that my output file will contain all the newly formatted data?

For clarification, I want to the result to be written as a result to an array, in one file output.

Also: my current code works just fine, but only for the first element[0], I just need to find a way to loop through the rest of the elements from the .JSON, and include them in my output

Answer

As I stated in the comments, try the forEach with an arrow function. Something like this:

let obj = require('./dataOut.json');
var fs = require('fs');

const source = [];

obj.forEach(o => {
  //json variables
  const scan_type = o.scan_type;
  const date_time = o.date_time;
  const source_address = o.source_address;
  const source_lat = o.source_lat;
  const source_lng = o.source_lng;

  //source contains the new object
  source.push({
    id: source_lat + source_lng,
    "source-lat": source_lat,
    "source-lng": source_lng,
    "source_address": source_address,
    x: {
      valueOf: function () {
        var latlng = [
          source_lat,
          source_lng
        ];
        var xy = map.FUNCTION_FOR_CONVERTING_LAT_LNG_TO_X_Y(latlng);
        return xy[0];
      }
    },
    y: {
      valueOf: function () {
        var latlng = [
          source_lat,
          source_lng
        ];
        var xy = map.FUNCTION_FOR_CONVERTING_LAT_LNG_TO_X_Y(latlng);
        return xy[1];
      }
    }
  });
});

//write out to file and stringify
fs.writeFile('./parentSource.json', JSON.stringify(source, null, 2), 'utf-8');

The o, at each iteration, will be an item of the array in your ./dataOut.json file.