A guide on converting JSON without using the "&quot" symbol

let newText = [{"name":"en3","value":234},{"name":"en4","value":135},{"name":"en1","value":335},{"name":"en2","value":310},{"name":"en5","value":1548}]

Is there a way to convert this data into JSON format without using the """ characters?

For example, like this:[{"name":"en3","value":234},and so on?

I attempted to use

JSON.parse('${resultData}'.replace(/"/g, '\\"'))
, but it resulted in an error saying
Uncaught SyntaxError: Unexpected token \

Answer №1

To modify your regular expression, use /"/g, '"'

This means that the correct code would be

JSON.parse('${dataResult}'.replace(/"/g, '"'))

Answer №2

let rawData = "[{"name":"en3","value":234},{"name":"en4","value":135},{"name":"en1","value":335},{"name":"en2","value":310},{"name":"en5","value":1548}]"

Modify the string to remove all instances of ":

let modifiedData = rawData.replace(/"/g, '"');

Then parse the modified data as follows:

let jsonData = JSON.parse(modifiedData);

Finally, iterate through the parsed array and log the values:

for(let i=0; i<jsonData.length; i++) { 
    console.log(jsonData[i].name);
    console.log(jsonData[i].value);
}

Answer №3

Eliminate the \\:

'${resultData}'.replace(/&quot;/g, '"')

Alternatively: refrain from encoding the " as &quot; initially.

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

How can I locally store 3D models and textures using three.js?

Currently working on a game using three.js, where each game level is on a different page. However, when transitioning from one level to another, the browser reloads the page which can be quite slow. Is there a way to store 3D models locally so they don&apo ...

How to Implement JSON Header in ASP.NET

I'm currently in the process of converting a PHP script to ASP.net C#. In PHP, it was easy to specify the content type with: header('Content-type: text/json'); header('Content-type: application/json'); How can I specify in my as ...

Checkbox in Angular FormGroup not triggering touched state

There seems to be an issue with the Angular form when checking if the form is touched, especially in relation to a checkbox element. Despite the value of the checkbox changing on click, I am seeing !newDeviceGroup.touched = true. I'm not quite sure wh ...

Code that changes every occurrence of a particular filtered selection of HREF values to a different value

When faced with the limitation in Firefox where links cannot be opened in a new tab if they have a HREF tag diverting to a function, it might be necessary to utilize a script to convert them to an actual HREF. Understanding the functionality of foo: func ...

obtain the image number by extracting a portion of the text

How can I extract the image number from a string using the sub-string function? I have applied the sub-string function to the property below. It returns 3 at the end in Chrome, but it does not work properly in Mozilla. Issue : After "-->", when I chec ...

Reading small JSON files from android assets experiences a significant lag

Currently, I am developing a straightforward Android application in Kotlin that will display categorized prayers to the user. In the assets folder, there are 5 JSON files, each approximately 10 KiB in size. For parsing these JSON files, I am utilizing Kla ...

Tips for keeping Fancybox from deleting the selected thumbnail

New to using fancybox and running into some issues.. starting to regret adding it. I have a row of thumbnails, all good, but when I click one it opens the THUMBNAIL instead of the actual link and on top of that, it DELETES the thumbnail from the DOM. I tr ...

What is the process for determining the default character length in a <p> tag based on its height and width?

I'm trying to determine the default length for the <p> tag in HTML. It should be displayed based on the height and width of the <p> tag. For example: Consider the following code snippet, <p style="height:300px;width:200px;"> </ ...

Error: The React component throws a TypeError because it is unable to read the property 'map' from an undefined source

I encountered the following error TypeError: Cannot read property 'map' of undefined at ListItemFactory.ts:84:57 at The specific line where the error occurs is: return announcementitems=json.value.map((v,i)=>( To provide mor ...

Tips for displaying a placeholder image within the Next.js image component

I am currently facing an issue with displaying images from API calls. To handle situations where there are no images or errors, I have implemented a code snippet which includes a placeholder image. However, the implementation seems to not be functioning as ...

Modifying the color of specific sections of SVG elements

Interested in utilizing the d3.js timeknots component, a svg visualization that includes line and circle elements. My goal is to implement a stopwatch animation that dynamically changes the color of the svg visualization over time. I am contemplating crea ...

Errors found during compilation when running the npm start command

After choosing to develop an app with React using VS Code, I initiated the process by running npm create-react-app ./, which was a success. However, when I proceeded to execute the command npm start, it resulted in compilation errors related to files suc ...

Issues arise with loading AngularJS directive properly within the context of service utilization

Currently, I am diving into the world of JavaScript and tackling a project that involves fetching data from MongoDB. My task is to write code in AngularJS to create a pie chart using Highcharts. Surprisingly, everything works smoothly when I solely use an ...

Is there a way to easily find the repository URL for each package listed in the package.json file using the command line interface

Imagine you have the following entries in your package.json file: "autoprefixer": "^7.0.1", "bower": "^1.8.0", "browserstack": "^1.5.0", "canonical-path": "0.0.2", "cheerio": "^0.22.0", "clean-css": "^4.1.2", "colors": "^1.1.2", "glob": "^7.1.1", "grunt": ...

What causes the UnhandledPromiseRejectionWarning while attempting to launch a browser with chromedriver?

I'm having trouble trying to launch a chrome browser and navigate to google.com using selenium chromedriver with node.js. Despite no version mismatch between the chromedriver and the browser, I encounter the following error message and the browser fai ...

Having to click twice in order to close the jQuery dialog box can be frustrating

I've been attempting to set up a div that pops up using jQuery dialog. Initially, when the user clicks on the button and opens the dialog, it closes with the first click. The second time they try to close the dialog, it will reopen the same popup, r ...

AngularJS Date Formatting

I am facing an issue with binding a simple date object created in JavaScript to an input control using AngularJS. It seems like the problem might be related to the format of the date. Can you please help me understand why this happens? This problem specifi ...

When using Json encode, Chinese characters are displayed as question marks

I have a list of names stored in a database using the utf8_unicode_ci collation. The Ajax dataType property is set as dataType:"json" Here's the PHP code snippet: $names = mysql_query($query); $name_arr = array(); while($name = mysql_fetch_assoc($ ...

Managing nested levels in Vue Draggable

Here is a link to the code sandbox: Nested Draggable Code Sandbox The nesting in this code is controlled through directives, specifically using v-if: <template> <draggable class="dragArea" tag="ul" :list="tasks" :g ...

Organizing various elements into separate divs with just one ajax request

I recently encountered an issue with my project involving an ajax call that was functioning correctly. $.get( 'accNoRealMovs1.jsp', {mode:"0"}, function(responseText){ $('#divAccMovementNR').html(responseTe ...