How can I use JavaScript to extract the contents of a file up to 'n' instances of a specific character and store it in a variable?

I have a text file that is being generated by the Apache Flink Tool. 1. My goal is to extract the contents of this file until it reaches the 999th "}," occurrence. 2. After extraction, remove the last comma(,) from the content. 3. Add "[" at the beginning and "]" at the end of the extracted content. 4. Save this modified content into a variable.

All of these steps need to be executed within a variable.

Is it feasible to perform this entire operation using Javascript?

The intention behind this task is to format the data as a JSON array suitable for plotting Highcharts, which can handle up to 1000 points at once.

Here are the initial contents of the txt file written by Flink:

{"temperSensorData":"28.489084691371996","temperSensorUnit":"celsius","timestamp":"1493270171424","timestamp2":"1493270171454","timestamp3":"1493270171454"},
{"temperSensorData":"28.48908469137112","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171522","timestamp3":"1493270171523"},
{"temperSensorData":"28.489084691371186","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171523","timestamp3":"1493270171524"},
{"temperSensorData":"28.489084691371595","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171524","timestamp3":"1493270171525"},
{"temperSensorData":"28.48908469137168","temperSensorUnit":"celsius","timestamp":"1493270171428","timestamp2":"1493270171529","timestamp3":"1493270171529"},
{"temperSensorData":"28.489084691371684","temperSensorUnit":"celsius","timestamp":"1493270171428","timestamp2":"1493270171529","timestamp3":"1493270171529"},

This is the desired (JSON) format after applying all the aforementioned actions:

[
    {
        "temperSensorData": "28.489084691371996",
        "temperSensorUnit": "celsius",
        "timestamp": "1493270171424",
        "timestamp2": "1493270171454",
        "timestamp3": "1493270171454"
    },
    {
        "temperSensorData": "28.48908469137112",
        "temperSensorUnit": "celsius",
        "timestamp": "1493270171426",
        "timestamp2": "1493270171522",
        "timestamp3": "1493270171523"
    },
    {
        "temperSensorData": "28.489084691371186",
        "temperSensorUnit": "celsius",
        "timestamp": "1493270171426",
        "timestamp2": "1493270171523",
        "timestamp3": "1493270171524"
    }
]

Answer №1

What do you think about this? It will generate an array structured like the following:

[
    [
        {999 ENTRIES}
    ],
    [
        {999 MORE ENTRIES}
    ]
    ...
]

var input = '{"temperSensorData":"28.489084691371996","temperSensorUnit":"celsius","timestamp":"1493270171424","timestamp2":"1493270171454","timestamp3":"1493270171454"}\n{"temperSensorData":"28.48908469137112","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171522","timestamp3":"1493270171523"}\n{"temperSensorData":"28.489084691371186","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171523","timestamp3":"1493270171524"}\n{"temperSensorData":"28.489084691371595","temperSensorUnit":"celsius","timestamp":"1493270171426","timestamp2":"1493270171524","timestamp3":"1493270171525"}\n{"temperSensorData":"28.48908469137168","temperSensorUnit":"celsius","timestamp":"1493270171428","timestamp2":"1493270171529","timestamp3":"1493270171529"}\n{"temperSensorData":"28.489084691371684","temperSensorUnit":"celsius","timestamp":"1493270171428","timestamp2":"1493270171529","timestamp3":"1493270171529"}'

splitInput = input.split('\n')
output = splitInput.map((item) => {
  return JSON.parse(item)
})

splitOutput = []

var chunk = 999;
for (var i=0,j=output.length; i<j; i+=chunk) {
    splitOutput.push(output.slice(i,i+chunk))
}

console.log(splitOutput)

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

socket.io / settings when establishing a connection

I'm facing an issue in my node.js / Express.js app where I need to pass parameters with the socket.io connection (saw a solution in another post). On the client side, here is a snippet of my code: edit var socket = io.connect('/image/change&ap ...

Performing a NodeJS MySQL query using chain promises

I have a set of 3 functions that I need to call step by step. For example, after calling the first function and getting a result, I must then call the second function and pass the parameter returned from the first call. Once the second call is completed, I ...

Function invoking React Hook

I'm a beginner to React JS and I've been learning by working on a project. I was creating a basic e-commerce UI with items and attempting to add items to a cart, but encountered an issue. The React Hook "useStateValue" is being called in the fun ...

Ways to extract innerHTML content from a loaded element generated by using the .load() method

Check out the code snippet below: template.html : <div id="nav"> Welcome to Space </div> layout.html : <div id="content"> </div> $('#content').load("template.html #nav"); ale ...

Is SPE.Group.tick experiencing issues with a faulty delta argument in Three.js / ShaderParticleEngine?

I recently started using the ShaderParticleEngine library in Three.js to set up particle emitters. After browsing through various code snippets online, I managed to get an emitter up and running. Initially, I thought it wasn't working as I could only ...

Hovering over a div will automatically scroll to the specified location

I recently inquired about a jQuery scrolling issue on this forum and the provided solution worked like a charm. However, I now require a modified version that directly scrolls to the specific div instead of navigating through all preceding ones. For examp ...

Unlocking the potential of dynamic form input values in Yii2 with JavaScript

How can I retrieve and calculate dynamic form input values in a JavaScript function? The current code only works for zero index input values. $this->registerJs( '$(document).ready(function(){ $("‪#‎quotationitem‬-0-unit_price").change(functi ...

Using Kotlin to work with JSON strings that contain line breaks and variables

In need of help adding linebreakers to a JSON string for improved readability. val myString = "some string" val myObjectJson = ` { "example1": "value", "example2": $myString }` This will allow me to utilize ...

What is the best way to showcase a chart using jquery?

Is there a way to incorporate trendlines or target lines in highcharts similar to fusion chart? I have been able to draw them successfully in fusion charts. Check out this fiddle link: http://jsfiddle.net/Tu57h/139/ I attempted to recreate the same in hi ...

The initialized Javascript array solely consists of undefined elements, without any of the expected values

I was under the impression that I knew how to declare JavaScript arrays, but in this particular script, I seem to be stuck in an infinite loop of undefined elements within the array. In my code, I define three arrays containing numbers—two with multiple ...

Having trouble coordinating a mongoose action to retrieve an array

Within the management of an employee app, I aim to segregate the business logic database operations from the main application file. A straightforward operation involves retrieving all employees from the database by using async/await for synchronization: m ...

The onClick event for the OnButtonSubmit function seems to be malfunctioning in ReactJS

When attempting to utilize the onClick event for a button in React, my goal is to simply have it log "clicked" in the console. However, this functionality is not working as expected. The Component where the onClick event is being called looks like this: ...

Kendo Grid with locked height

Using a grid with some elements locked, we have established a CSS-defined minimum and maximum height for the grid. .k-grid-content { max-height: 400px; min-height: 0px; } An issue arises when setting the height of the locked grid. If the grid&a ...

Tips for preserving the state of the Material-UI AutoComplete during component re-renders?

Currently, I am utilizing the Material-UI v4 AutoComplete component along with the renderOption prop in order to display a checkbox item option. The issue arises when the onChange event occurs and updates a hook in the parent component causing a re-rende ...

Why are the links in the navgoco slide menu failing to function properly?

I utilized a demo from jQueryRain to create a collapsible menu using jQuery. However, after completion, I discovered that none of the links were functioning properly. Upon visiting the documentation page, I noticed that many users were encountering the sam ...

Is there a way to set a maximum limit for a checkbox form within bootstrap 4 button-groups?

My form includes questions that utilize checkboxes and radio buttons. The checkbox questions have a maximum number of selections allowed before proceeding to the next question. I am using Bootstrap buttons for both checkboxes and radio buttons. For more i ...

How can I restrict input to only numbers and one decimal point using JavaScript?

The following code I have is not functioning as expected: function validateInputValue(element) { var regex = /\D$/i; // Important: avoid white spaces in password if(regex.test(element.value)) { alert("Please check your input format"); e ...

Creating a Sum Column in a DataTables Editor

https://datatables.net/forums/discussion/45999 Is there a way to create a Total column which sums up three columns (No.1, No.2, No.3)? Here is an example table structure: ------------------------- No1 | No2 | No3 | Total | ------------------------- 4 ...

Executing two SQL queries simultaneously in NodeJS can be achieved by using a single statement

app.get("/total", function(req,res){ var q = "SELECT COUNT(*) AS new FROM voters_detail WHERE parties LIKE '%BJP%'"; connection.query(q, function(err, results){ if(err) throw err; var hello = results[0].new; res.send("BJP Was Voted By ...

Oops! Looks like there was a glitch in the server for the application. The page you are looking for cannot be found. Error code: HTTP 404. Requested URL: /

Description: Oops! The page you are trying to access cannot be found. It may have been removed, renamed, or is currently unavailable. Please double-check the URL for any errors. Requested URL: /UsersPage/undefined I've got this AJAX script that is s ...