What are some techniques for transforming text into JSON format?

Beginner inquiry:

I'm struggling with manipulating text using JavaScript. Here is the text I want to transform:

"5555 55:55: John: New York
 6666 66:66: Jack: Los Angeles"

My desired output after manipulation:

[{ name:"John", address:"New York", number:"5555 55:55"},{ name:"Jack", address:"Los Angeles", number:"6666 66:66"}]

I am unsure how to inform JavaScript about new lines, names indicated by "xxxx:", and addresses immediately following "xxxx:(space)."

Your assistance on this matter would be greatly appreciated. Thank you in advance.

UPDATE

Current text snippet:

var text = "5555 55:55: John: New York \n 6666 66:66: Jack: Los \n Angeles 7777 77:77: Smith: South Park: 3321"

Here is my current code snippet:

var result = text.match(/\d\d\d\d \d\d:\d\d: [a-zA-Z]+: /g)
                .map(function (x) {
                    x = x.split(': ');
                    return {
                        number: x[0],
                        name: x[1],
                        address: x[2]
                    };
                });

The issue I'm encountering is that my regex pattern is incorrect, and the 'split' function at ': ' fails when handling addresses that contain colons. Any guidance to resolve this would be much appreciated.

Answer №1

Firefox has no problem with the following:

"5555 55:55: John: New York\n 6666 66:66: Jack: Los Angeles"
 .split('\n')
 .map(function(x) {
  x = x.split(': ');
  return {
    number: x[0],
    name: x[1],
    address: x[2]
  };
});

If you are using other versions, be aware that some may not support map and you might need to find an alternative method.

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

What could be causing the issue of being unable to connect to the NodeJS express server from any network?

Imagine having a web server running on port 3001 with the IP address 23.512.531.56 (obviously not a real one) and then switching to another network, like your neighbor's. Now, when you try entering 23.512.531.56:3001 in Chrome, why doesn't the se ...

Troubleshooting Issue with jQuery replaceWith in Loop

Trying to make it so that when the update button is clicked, the text on the left side becomes an input box: Click the update button and the text on the left will be an input box However, instead of just one input box appearing, all the text on the left s ...

Interacting with a WebGL globe: accessing database information and displaying pop-up windows when clicking on specific locations on the globe

I am working on creating a WebGL Globe similar to the one found at this link: Currently, I have been using this example as a reference point: Here are the specific project requirements that I am aiming to achieve: The website needs to be able to manage ...

Solution to trigger CSS :hover to refresh following a transition (such as expanding a menu)

While there are existing discussions on this topic, I am presenting my query for two specific reasons: It introduces a potential alternative solution The demo code could be helpful to individuals looking to emulate a menu Following a CSS transition, the ...

The Three.js GLSL shader encountered a compilation error

I encountered a compile error message: THREE.WebGLShader: Shader could not compile. I attempted to use shaders from shaderfrog.com, but unfortunately they did not compile correctly. To troubleshoot, I added my new vertex and fragment shaders to the DOM a ...

Having trouble initiating a "curl:localhost:3000" connection, receiving a URI Error message

Recently delving into the realm of node js, I have embarked on a journey to start up a server and experiment with an app designed to trim URLs. However, I find myself at an impasse. Environment: Windows Text Editor: VSCode Below is my code for index.js ...

Managing numerous API requests in React Native

As I work on implementing a search field, I've encountered a challenge. Whenever a user enters text in the search field, a timer resets to 300 ms before an API call is sent to fetch autocomplete results. After receiving these results, the app then wai ...

JavaScript - filter out values not included in specified list of attributes

I am seeking a technique that, when provided with a list of attributes, retains only the values associated with keys present in the list. For instance: attrs = ['a', 'b', 'c'] obj = {'a': 1, 'b': 2, &apos ...

AngularJS - sorting JSON data based on key values

I am working with a JSON data set that I need to filter based on the selected option value. The select input is bound to an ng-model, but for some reason, the filter isn't functioning properly. Can anyone spot what mistake I might be making? This is ...

Finding the latitude and longitude coordinates of a point at a specific distance from another using Node.js or JavaScript

My task involves determining the square area based on a latitude and longitude (x,y) as shown in the provided figure. To accomplish this, I must calculate the coordinates of the remaining three corners by adding 10 kilometers to each side. I will be using ...

What is the best method for deleting the div with id "__next" in Next.js?

I'm currently working on a website using Next.js and I'm aiming to create a header with a position: sticky; effect. Nevertheless, Next.js automatically inserts a div with the attribute id="__next" at the top of my website without my co ...

Is it possible to dynamically assign trigger and target divs to bPopup?

On our ColdFusion page, we have a dynamic list of paired divs created using a loop over a query. For example, each pair consists of internshipHandleX and internshipHiddenX: <div id="internshipHidden#internship.currentrow#" class="hidden pop-up"> We ...

linking to a page that shows the user's chosen option from a dropdown menu

One of the challenges I encountered was creating a feature that allows users to share a specific selection from multiple dropdown lists on a page. Essentially, my goal was to enable users to send a link that would direct others to the same page with the ex ...

Sending information in Json format back to an Ajax request

I have a method in the Model-View-Controller (MVC) framework where I submit data and expect to receive some processed data back. The MVC method I am posting to returns JSON data. [HttpPost] public JsonResult GetCalculateAmortizationSchedule() { var da ...

Troubleshooting Problems Arising from PHP, Ajax, and SQL Integration

I've been encountering an issue that seems simple, but I haven't been able to find a solution specific to my problem in the forums. The problem arises when I try to display a table of hyperlinks, each linked to an AJAX method with an 'oncli ...

Struggling to reset the jscrollpane scroller

When using jscrollpane in my horizontal division to enable scrolling, I encounter an issue when loading data with ajax. The scrollbar doesn't appear until the browser width is changed. To address this, I currently utilize the following method to rein ...

Utilizing the map() function to parse a Json object

Looking for guidance with working with a JSON object structured like this: {"Title":"asb","Date":"2019","Other":"not important"} How can I correctly read this object and render it in <ul><li> format? Please Note: I have attempted to assign th ...

Particles.js fails to persist as I scroll down the HTML page

After creating a website page, I incorporated Particles.js for the background. However, when I scroll down, the particles seem to be confined to the initial corner of the screen and do not continue downward. Here are my current particle settings: #particl ...

Docz: Utilizing Typescript definitions for props rendering beyond just interfaces

We are currently using Docz to document our type definitions. While it works well for interfaces, we've run into an issue where rendering anything other than interfaces as props in Docz components doesn't seem to display properly. I'm seeki ...

Checking for the existence of a file in JavaScript

I rely heavily on the scripts and images on my website, but the problem is that everything gets saved to the cache. This means that users can't see updates unless they clear their cache or open the site in private browsing mode. I'm working on cr ...