Transform a log file into a JSON structure

In my log file titled request.log, the following entries are present:

[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"
  }
}

I am seeking guidance on how to extract and convert this log file content into an array of JSON objects like so:

[
  {
     "method": "POST",
     "url": "https://test.co/otp"
  },
  {
     "method": "POST",
     "url": "https://test.co/login"
  }
]

Answer №1

To transform them into JSON, you need to execute a script. In javascript/nodejs, you can achieve this by:

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 this doesn't suit your log files, adjust the regex to match your specific scenario.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error message: Unable to load image from local source in Vue application

I am currently working on creating a simple dice roll game in VueJs. However, I encountered an issue with using the dice images as it keeps giving me a 404 error. When I attempted to use require(), it stated that the function was not defined. After some t ...

Automatically load content using AJAX when scrolling within a HTML5 segment

Recently, I've been developing a website that dynamically loads new content from a MySQL database as the user scrolls. However, I've encountered an issue where the new content is loaded even with minimal scrolling, rather than only when reaching ...

What does the symbol ^R represent in WebStorm?

I'm attempting to execute my node program using the keyboard shortcut that WebStorm offers. However, when I press Control as instructed, nothing seems to happen. Am I understanding the shortcut correctly? https://i.stack.imgur.com/kMpyf.png ...

The following MongoDB errors unexpectedly popped up: MongoNetworkError: connect ETIMEDOUT and MongoServerSelectionError: connect ETIMEDOUT

I've been working on a React and NextJS App for about a month now, utilizing MongoDB as my database through MongoDB Atlas. I'm currently using the free version of MongoDB Atlas. For the backend, I rely on NextJS's api folder. Everything wa ...

What is the method for specifying a null value in Typescript?

I'm curious if this code snippet is accurate, or if there's a better way to define it. Is there an alternative to using error!? I'm unsure of its meaning and would appreciate clarification. ...

The WhatsApp chatbot is not compatible with Heroku's platform

Currently working on developing a WhatsApp chatbot and deploying it on Heroku. However, I seem to be stuck at this particular point. https://i.stack.imgur.com/IROOM.png Encountering errors while trying to launch the browser process for the chatbot de ...

Exploring GatsbyJs: Optimal Strategies for Storing Strings and Text on Static Websites

Currently in the process of building a website using Gatsby JS and Material UI. I'm wondering about the best approach for storing the site content. This website will serve as a promotional platform for a small business. Should I store the text direct ...

Can I assign a value from the tagModel to ngx-chips in an Angular project?

HTML code: <tag-input class="martop20 tag-adder width100 heightauto" [onAdding]="onAdding" (onAdd)="addInternalDomain($event)" type="text" Ts code: addInternalDomain(tagTex ...

Error: The node is unable to parse JSON data through the API

After loading a JSON file as a string, attempting to parse it back to JSON and send it as a response: router.get('/todos', (req,res) =>{ let todos = fs.readFile('todos.json', 'utf8',(err, data) =>{ if (err) ...

How to populate an ExtJS 3.4 combobox with local JSON data in a few simple steps

I am utilizing ExtJS 3.4 and in need of populating a combobox with specific data obtained from a previous XMLHttpRequest, stored in a variable as follows: my_variable = "[{"cod_domini":"1","nom_domini":"Sant Esteve de Palautordera"},{"cod_domini":"2","no ...

jQuery not being applied to dynamically added dropdown element

I am currently utilizing bootstrap and jquery within my react project. I have a button that, when clicked, should transform into a dropdown field. The dropdown functions properly when placed statically, but the functionality is lost once it is dynamically ...

Achieving optimal performance with scoped CSS in Vue.js

As I work on creating a new app using VueJs, I have noticed the implementation of "css scoped" as shown below: <style scoped> .example { color: red; } </style> <template> <div class="example">hi</div> </template> ...

Using Apache to proxy Socket.io requests

When setting up proxying with Apache to enable the use of socket.io in my Node.js application, an error message appears on the client side: WebSocket connection to 'wss://example.com/tools/socket.io/?EIO=3&transport=websocket&sid=eOGwJSC14TTW ...

Using the spread operator in ES6 allows for arguments to be placed in a non-con

When working in nodeJS, my code looks like this: path = 'public/MIN-1234'; path = path.split('/'); return path.join( process.cwd(), ...path); I was expecting to get: c:\CODE\public/MIN-1234 but instead, I got: `‌publ ...

Tap the key in Robot.js in an asynchronous manner

I'm using robot.js to execute a high volume of key tap commands rapidly in my program. Here's a brief overview of how it currently works: ...for in elements setTimeout(() => { console.log(element.deltaTime) // Records the time that the k ...

Having difficulty entering text in the modal text box and updating it with a new state

Within my render method, I am utilizing the following model: { showEditModal && <Modal toggleModal={this.togglePageModal} pageModal={true}> <h2 style={{ textAlign: "center", width: "100%" }}> ...

Endless Loading in Node.js on Google App Engine

I have deployed a standard nodejs app (free tier) using cloud shell, but when I try to access https://[ID].du.r.appspot.com/ , it keeps loading indefinitely. app.js: const express = require('express'); const path = require('path'); co ...

One project contains a pair of React instances

I am currently working on a React Web App and recently encountered an issue with an 'invalid hook call' error. Upon further investigation, I discovered that there are duplicate copies of the React library in my project, including within the CSS f ...

An exemplary chat application that utilizes sessions for user authentication in a node.js environment with socket.io and express

Is there an example available that stores sessions and uses those sessions for all opened windows, allowing one user to connect to a specific room only once? This application will treat phpsessions as node.js sessions, but I am struggling to restrict acce ...

Can we include an option to display all pages in one row on a single page?

Is it possible to include an option to display all rows alongside the current options of 10, 15, and 100? I've been unable to locate this feature in the documentation: Check out the Codesandbox example: https://codesandbox.io/s/muidatatables-custom-t ...