1

I have log file named request.log this is the content of the log file

[2022-06-30T09:56:40.146Z] ### POST https://test.csdf/auth/send_otp  
    
 {
  "method": "POST",
  "headers": {
    "User-Agent": "testing",
    "Content-Type": "application/json"
  }
}
[2022-06-30T09:56:40.668Z] ### POST https://test.csdf/auth/login  
    
 {
  "method": "POST",
  "headers": {
    "User-Agent": "testing",
    "Content-Type": "application/json"
  }
}

how can I parse the log file and transform it into array json object like this

[
  {
     "method": "POST",
     "url": "https://test.co/otp"
  },
  {
     "method": "POST",
     "url": "https://test.co/login"
  }
]
4
  • 1
    How are you generating this logs ?? Commented Jun 30, 2022 at 12:22
  • Neither https://test.co/otp nor https://test.co/login are actually anywhere to be found in your sample data. So do you actually want to manipulate those URLs from the log file ... or just pay a little more attention when asking? Commented Jun 30, 2022 at 13:26
  • If you want the URLs as they are - then just split this into individual lines and loop over them; check if a line starts with [, and if so, extract method & URL, if not, skip it and process the next one ... Commented Jun 30, 2022 at 13:28
  • @NS23 I actually have this http request function, and I put fs.createWriteStream() in there to log the request payload. Commented Jul 1, 2022 at 2:53

1 Answer 1

2

You have to run some kind of script to convert them to JSON. In javascript/nodejs you could do something like this:

const logFileContents = `[2022-06-30T09:56:40.146Z] ### POST https://test.csdf/auth/send_otp  
    
{
 "method": "POST",
 "headers": {
   "User-Agent": "testing",
   "Content-Type": "application/json"
 }
}
[2022-06-30T09:56:40.668Z] ### POST https://test.csdf/auth/login  
   
{
 "method": "POST",
 "headers": {
   "User-Agent": "testing",
   "Content-Type": "application/json"
 }
}`;

function convertToJSON(logs) {
  return logs
    .match(/\[.+\]\s\#+\s\w+\s.*\n/g) // fetch lines to parse
    .map((line) => line.split('###')[1].trim().split(' ')) //isolate method/url
    .map((log) => ({
      method: log[0],
      url: log[1],
    }));// convert array to object
}

console.log(JSON.stringify(convertToJSON(logFileContents), null, 4));

if it doesn't work for your log files, just fix the regex to match your case

Sign up to request clarification or add additional context in comments.

1 Comment

this is what I'm looking for. I'm struggling on how to split it and map it into a new JSON. Thankyou!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.