How can you extract key-value data from a text file using a given list of property names and then convert it into JSON format?

I am currently working on creating a JSON file that stores matched words from a text like this

servicepoint ‏ 200135644 watchid ‏ 7038842

Each servicepoint and watchid should be added to the Object table only once using the following code:

function readfile() {
  Tesseract.recognize('form.png', 'ara', {

    logger: m => console.log(m)

  }).then(({ data: { text } }) => {
    console.log(text); /* this line here */
    var obj = {
      table: []
    };
    const info = ['servicepoint', 'watchid'];
    for (k = 0; k < info.length; k++) {
      var result = text.match(new RegExp(info[k] + '\\s+(\\w+)'))[1];
      obj.table.push({
        servicepoint: /* Need to add the number after servicepoint here */ ,
        watchid: /* Also need to include the number after watchid in the Object table*/
      });
    }
    var json = JSON.stringify(obj); /* Convert the object table to a JSON file*/
    var fs = require('fs'); /* Then write a JSON file containing the data*/
    fs.writeFile('myjsonfile.json', json, 'utf8', callback);
  })
};

Answer №1

As mentioned in one of my previous comments...

It is possible to omit the unicode flag and convert the pattern back to /servicepoint\W+(\w+)/, which closely resembles the original code provided by the OP. The only adjustment needed is to replace \\s+ with \\W+.

In addition to changing the suggested pattern, it would be beneficial to transform the OP's thenables into clearer tasks such as segregating file-reading from data parsing/extraction to JSON conversion/writing.

I'd also like to clarify that the desired format based on data.table cannot be an array but must adhere to a pure key-value structure (object). This means you can either aggregate a single object or push individual properties into an array when iterating over property names. In the current scenario, the OP attempts to create a multi-entry object while pushing it simultaneously.

The subsequent code snippet demonstrates the approach, leveraging async-await syntax and simulating file reading and writing processes using mock data.

async function readFile(fileName) {
  console.log({ fileName });

  // return await Tesseract.recognize(fileName, 'ara', {
  // 
  //   logger: m => console.log(m)
  // });

  // fake it ...
  return (await new Promise(resolve =>
    setTimeout(
      resolve,
      1500,
      { data: { text: 'servicepoint ‏ 200135644 watchid ‏ 7038842'} }
    )
  ));
}
/*async */function parseDataFromTextAndPropertyNames(text, propertyNames) {
  console.log({ text, propertyNames });

  return propertyNames
    .reduce((table, key) =>
      Object.assign(table, {

        [ key ]: RegExp(`${ key }\\W+(\\w+)`)
          .exec(text)?.[1] ?? ''

      }), {});
}
async function writeParsedTextDataAsJSON(fileName, table) {
  console.log({ table });

  // const fs = require('fs');
  // fs.writeFile(fileName, JSON.stringify({ table }), 'utf8', callback);

  // fake it ...
  return (await new Promise(resolve =>
    setTimeout(() => {

      console.log({ fileName, json: JSON.stringify({ table }) });
      resolve({ success: true });

    }, 1500)
  ));
}

console.log('... running ...');

(async () => {
  const { data: { text } } = await readFile('form.png');

  const data = /*await*/
    parseDataFromTextAndPropertyNames(text, ['servicepoint', 'watchid']);

  const result = await writeParsedTextDataAsJSON('myjsonfile.json', data);

  console.log({ result });
})();
.as-console-wrapper { min-height: 100%!important; top: 0; }

Answer №2

If you need to extract the necessary variable values for the keys "servicepoint" and "watchid", consider utilizing the String.match method.

For assistance with creating a match pattern, check out this link.

After obtaining the data points, convert them into a JSON format like so:

{servicepoint: 1323, watchid: 234}

In case you have multiple rows of similar data, store each key-value pair in an array. Finally, use JSON.stringify(dataArray) to generate valid JSON text for saving to a file.

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

Can a form be selected and submitted using only its class as a selector?

When trying to select and submit a form using only the class as a selector, I encountered an issue: $("form.class").submit(...) doesn't seem to work... however: $("form#id").submit(...) works perfectly fine. Any idea what could be causing this di ...

Innovative manipulation of arrays using Javascript

Let's say I have some sample input data: data = [ { color : 'Red', number : 5}, { color : 'Blue', number : 3 }, { color : 'Green', age : 8 }, { color : 'Red', number : 7 } ] and I am looking to combine ...

Craft a circular design with an Arc and a Pie using the DIV DOM element

After creating an ellipse using the div DOM element, here's how I did it: var body = document.querySelector('body'); var div = document.createElement('div'); div.style.borderRadius = '50%'; div.style.border = '1px s ...

When using QML, functions like Object.keys, Object.values, and JSON.stringify may return unexpected empty results

I have written a code that exposes a C++ object to QML, and I want to verify the declared properties of the object using native JS methods. However, they are not working as expected. I created a method called FizzBuzzDerived.properties, which functions cor ...

Matching a specific HTML element with a regular expression

I need assistance creating a regular expression to match the HTML code below: <span class="hidden_text">Some text here.</span> My current attempt is only partially successful as it sometimes captures additional content after the span. Here&ap ...

What is the best method to retrieve the current time in minutes using the Timer component?

I have integrated the react-timer-hook library into my Next.js application to display a timer. The timer is functioning correctly, but I am encountering an issue where I cannot retrieve the current elapsed time in minutes during the handle form event. My g ...

Having trouble aligning two contact forms side by side in the center

I've been researching CSS techniques for hours, experimenting with different methods to align them horizontally, but I'm struggling to achieve the desired result. Currently, I am delving into the world of UI/UX design and finding inspiration on ...

Transforming the date from yyyy-mm-dd to Tuesday, the 25th

My variable is currently in the format: yyyy-mm-dd. I am looking to convert it into the format Tuesday 25th using JavaScript (specifically, the jQuery library). I attempted the following: var now = new Date('2013-06-25').format("l jS"); ...

Error: Unable to retrieve AJAX response

I have reviewed numerous posts regarding this issue and I am unable to understand why I am still receiving 'undefined' for responseText. The expected json format should be {"token": "ghargaeorigjaoregrjarjegijra[pgjpraejgprjgpkfp5p34i5483te8q9rut ...

What causes the literal loop to produce additional commas in its output?

Having some trouble understanding template literals in javascript, particularly the syntax. Whenever I run this loop, it seems to output extra commas between each iteration: character = () => { const traits = ["Knowledge", "Agility","Strength", "Ch ...

Code for activating datalist on Safari and Opera browsers

As I was wrapping up work on a website, I realized that Safari doesn't support the datalist feature in HTML. This was quite troublesome since a significant portion of the site's audience consists of individuals who may not be very tech-savvy, mak ...

What is the best method for retrieving an array stored within a Json object?

Upon receiving a JSON response, my goal is to extract all project names listed within. After trying the usual method of iterating through arrays, I realized that in this case, "d" is not an array itself. How can I go about retrieving the desired result? ...

Display all items that contain a specified string using JavaScript

I'm in need of some assistance with a problem that I've been struggling with. I have several JavaScript objects containing different data pieces as shown below:- Object {id: 1, shopcounty: "cornwall", shopaddress: "the cycle centre,<br />1 ...

The behavior of the Ionic checkbox in version 5 seems to be quite delayed

I am facing an issue with binding the checked attribute value on an ion-checkbox, as the behavior seems to be delayed. In my .ts file, I have an array variable named user_id. In my checkbox list, I am trying to populate this array based on which checkboxe ...

Issue with delayed chaining of class addition and removal in JQuery not functioning as expected

Here is the code snippet: $(document).ready(function () { $('.current-visitors').delay(3000).queue(function () { $(this).addClass('show').delay(1000).queue(function () { $(this).removeClass('sho ...

Demonstrate the utilization of JQuery to unveil a secondary menu

I need assistance in implementing a sub-menu that automatically appears within 2 seconds after the page loads, instead of requiring user interaction. I am currently utilizing JQuery, which serves as the core framework for my website. It is crucial for this ...

Validating group radio buttons that have been checked

I possess: <div class="group"> <input type="radio" name="myradio" value="1">a <input type="radio" name="myradio" value="2">b </div> <div class="group"> <input type="radio" name="two" value="3">a <input ty ...

Unexpected issues have arisen with the $.ajax function, causing it

I am facing an issue where I can see the loader.gif but unable to view avaiable.png or not_avaiable.png in my PHP file. What could be causing this problem? $(document).ready(function()//When the dom is ready { $("#inputEmail").change(function() { ...

What is the best way to modify the appearance of the button?

I'm attempting to change the appearance of buttons at the top of a webpage to be square and blue. I have jQuery code for this purpose, but it doesn't seem to be functioning correctly. Here is the snippet of code I am using: $(document).ready(fu ...

Using JSDoc and Visual Studio Code: Writing documentation for a function that is being passed as an argument to another

Struggling to properly document the input parameters for a JavaScript function using JSDoc. The documentation suggests using the @callback comment, but Visual Studio Code (VSCode) doesn't seem to recognize it. In VSCode, the intellisense for the loca ...