Converting a string into an array of objects using Angular TypeScript

Can anyone help me with converting the following string into an array of objects?

{"Car":
"["
   {"Carid":234,"CompanyCode":null}","
   {"Carid":134,"CompanyCode":"maruti"}","
   {"Carid":145,"CompanyCode":"sedan"}","

"]"
}

I attempted using JSON.parse, but it threw an error saying Unexpected token '{ ' at position 11

Then, I tried using eval, but it also resulted in an error stating Unexpected token ':'

Answer №1

To tackle this type of issue, one can utilize regular expressions for a solution.

let s = '{"Car":"[" {"Carid":234,"CompanyCode":null}"," {"Carid":134,"CompanyCode":"maruti"}","{"Carid":145,"CompanyCode":"sedan"}",""]"}';


let matchReplace = [['"\\,"',','],['"\\["','['],['\\,"\\]"',']']];
matchReplace.forEach((reg)=>{
  let regularExp = new RegExp(reg[0],'g');
  s = s.replace(regularExp,reg[1])
});

console.log(JSON.parse(s));

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

Enhance your React Typescript High Order Component by incorporating additional properties and implementing them

I am in the process of creating a React HOC with specific requirements: It should take a component as input, modify the hidden property (or add it if necessary), and then return the updated component The rendered component should not display anything whe ...

Expand or collapse Angular Material Accordion depending on selected Radio button choice

Is it possible to use a mat-accordion with radio buttons where each panel expands only when its corresponding radio button is selected? I have the radio buttons functioning correctly, but the panels are expanding and collapsing with every click rather than ...

React-router-dom TypeScript error when defining the type of the prop parameter in the loader

I'm having trouble with the params prop in the loader prop within the routes. I've defined the params in TypeScript, but I'm getting errors that I don't understand. Any help would be appreciated, thanks in advance. I tried to use the Cu ...

What is the best way to test an oclif CLI tool that interacts with a Rest API

How can I efficiently test the TypeScript code I've written for an Oclif CLI that interacts with a Node.js and Express.js REST API? I'm currently using Mocha/Chai for testing, but I'm struggling with testing the specific command code from my ...

Unable to refresh JSON information

My current task involves refreshing a "Bar chart" with JSON data on the server. However, I am facing an issue where Android is unable to update that JSON data even though I have tried using a runnable method. The fetched data remains constant and does not ...

Using Two Unique Typeface Options in Material UI for ReactJS

Currently, in my React App, I am utilizing the Material UI library with Typescript instead of regular Javascript. I've encountered a small hurdle that I can't seem to overcome. The two typefaces I want to incorporate into my app are: Circular-S ...

Cease the firing of ion-change until the values have been successfully loaded from storage

My settings page contains multiple ion-toggle's. There's an onChange method to update local storage when toggled. Standard procedure involves checking existing values in storage on page load and mapping them to the toggles using ngModel. <tab ...

CPU usage spikes after launching a Cordova project in Visual Studio 2015 RTM

If you're looking for the source code of the project, you can find it at https://github.com/Yaojian/Ionic-TypeScript-Starter/. I decided to create a Visual Studio project by forking https://github.com/Justin-Credible/Ionic-TypeScript-Starter/ and fol ...

Intermittent issue with Angular 2 encountered while following the Hero Editor tutorial on angular.io

I am encountering an occasional error in the console while following the angular.io tutorial using Mozilla Firefox. The error does not seem to impact the functionality or rendering of my application, and it only happens sporadically. If you could provide ...

Angular is not programmed to automatically reflect updates made to my string array

let signalRServerEndPoint = 'https://localhost:44338'; this.connection = $.hubConnection(signalRServerEndPoint); this.proxy = this.connection.createHubProxy('MessagesHub'); this.proxy.on("ReceiveMessage", (message) => { ...

Personalize the appearance of autocomplete in Angular 2 using a dynamic <ng-content> tag

Currently, I am working on developing an autocomplete feature for a component. However, I am facing difficulties in handling the display of the items. I experimented with using ng-content to render the item but encountered failures. What I aim for is to ut ...

Tips for effectively integrating an admin panel into an Angular project

After building a website with Angular 6, I now need to develop an admin panel to make the site dynamic. What would be the best approach for this task - creating a separate Angular app for the admin panel or incorporating it within the existing website ap ...

Express.js receiving JSON POST data with AngularJS incorrectly interpreted as keys instead of values

I am facing an issue while trying to make a POST request with JSON data from angularjs to node/express. The problem is that all the data is appearing in the KEY of the req.body instead of as key value pairs. Although I found a similar question addressing ...

Using RXJS within the pipe operator to make numerous HTTP requests

In my project, I have set up 3 API endpoints - candidates, vacancies, and interviews. { "candidates": [ { "id": 1, "name": "Serj" }, { "id": 2, "name": "Alex" } ], " ...

Create Open Graph meta tags dynamically using the MEAN stack, including Angular 2+

I'm currently working on creating unique OG tags for specific item-detail pages. I am using the meta module for Angular from https://github.com/nglibs/meta to generate these meta tags, which work perfectly fine in browsers. However, when it comes to F ...

Tips for Achieving Observable Synchronization

I've encountered a coding challenge that has led me to this code snippet: ngOnInit(): void { this.categories = this.categoryService.getCategories(); var example = this.categories.flatMap((categor) => categor.map((categories) = ...

What is the syntax for invoking the json.decode() function?

Currently, I am working on a code snippet that is responsible for converting a JSON object into iCalendar format. In order to achieve this, I am developing an iCalendar template and then incorporating the data from the JSON object into it. However, one of ...

Automatically install peer dependencies in the client-side library

Currently, I am working on a React package/library that I have bundled using Rollup. Within this package, there are various components that will be utilized in an Angular application. To manage the dependencies, I have defined certain packages as peerDepe ...

Is Angular 4 failing to set headers properly or is Express.js searching in the wrong place?

When interacting with an Express.js API, I encountered a issue regarding the handling of auth tokens. The problem arose when sending the token in the request headers using Angular 4 compared to Postman. In Postman, setting the header named 'Authorizat ...

After updating next.config, the NextJS + NextAuth middleware doesn't seem to be triggered

I'm currently utilizing <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b0ded5c8c49dd1c5c4d8f0849e81889e87">[email protected]</a> & <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemai ...