Parsing log file to JSON object

Viewed 339

Does anyone know what the best way would be to parse a log file (Salesforce) into a structured JSON object

There are certain sections in the log file that can be identified, like EXECUTION_STARTED,EXECUTION_FINISHED, CODE_UNIT_STARTED CODE_UNIT_FINISHED and many more

There are also time information that I would like to have in the JSON object

Are there any libraries available in nodejs that could be used to accomplish this ?

I was looking into antlr4 but it seems quite complex

-Jani

1 Answers

Looks like a CSV -> JSON conversion

// Reading the file using default
// fs npm package
const fs = require("fs");
csv = fs.readFileSync("CSV_file.csv")
 
// Convert the data to String and
// split it in an array
var array = csv.toString().split("\r");
 
// All the rows of the CSV will be
// converted to JSON objects which
// will be added to result in an array
let result = [];
 
// The array[0] contains all the
// header columns so we store them
// in headers array
let headers = array[0].split(", ")
 
// Since headers are separated, we
// need to traverse remaining n-1 rows.
for (let i = 1; i < array.length - 1; i++) {
  let obj = {}
 
  // Create an empty object to later add
  // values of the current row to it
  // Declare string str as current array
  // value to change the delimiter and
  // store the generated string in a new
  // string s
  let str = array[i]
  let s = ''
 
  // By Default, we get the comma separated
  // values of a cell in quotes " " so we
  // use flag to keep track of quotes and
  // split the string accordingly
  // If we encounter opening quote (")
  // then we keep commas as it is otherwise
  // we replace them with pipe |
  // We keep adding the characters we
  // traverse to a String s
  let flag = 0
  for (let ch of str) {
    if (ch === '"' && flag === 0) {
      flag = 1
    }
    else if (ch === '"' && flag == 1) flag = 0
    if (ch === ', ' && flag === 0) ch = '|'
    if (ch !== '"') s += ch
  }
 
  // Split the string using pipe delimiter |
  // and store the values in a properties array
  let properties = s.split("|")
 
  // For each header, if the value contains
  // multiple comma separated data, then we
  // store it in the form of array otherwise
  // directly the value is stored
  for (let j in headers) {
    if (properties[j].includes(", ")) {
      obj[headers[j]] = properties[j]
        .split(", ").map(item => item.trim())
    }
    else obj[headers[j]] = properties[j]
  }
 
  // Add the generated object to our
  // result array
  result.push(obj)
}
 
// Convert the resultant array to json and
// generate the JSON output file.
let json = JSON.stringify(result);
fs.writeFileSync('output.json', json);

Change "CSV_file.csv" above to whatever your CSV file is. Save the js file as app.js and run using node app.js

Source: https://www.geeksforgeeks.org/how-to-convert-csv-to-json-file-having-comma-separated-values-in-node-js/

Related