Attempting to eliminate quotation marks from JSON keys that are stored in an array

I am working with an array of JavaScript objects that represent musical notes in my front-end development. To store this data, I had to convert the object array into a JSON string using JSON.stringify(objectArray) before placing it into a hidden input field. However, this process automatically encloses all the keys in double quotes as shown below:

[
  {"class":"barline","symbol":"standard","barline":true,"newSystem":true},
  {"class":"note","rhythm":"half","duration":0.5,"symbol":"flag","hand":"R","newbar":true,"rebel":false},      
  {"class":"note","rhythm":"half","duration":0.5,"symbol":"flag","hand":"R","endbar":true,"rebel":false},
  {"class":"barline","symbol":"standard","barline":true},
  {"class":"note","rhythm":"half","duration":0.5,"symbol":"flag","hand":"R","newbar":true,"rebel":false}
]

Before filtering the parameters in Rails, I use

JSON.parse(params[:score][:notes])
to convert the string back to a proper JSON array for storage in MongoDB (I'm using Mongoid).

Even though it is commonly advised to include keys in quotes, I prefer using dot notation to access values in JavaScript. Do you recommend switching to bracket notation or can you suggest a simple function that would remove the quotes from the keys before sending them to the hidden input?

Answer №1

In order to meet linting requirements, I needed a regex that identifies quotes preceding semicolons within a string:

"(\w*)":

To then replace these instances with just the text minus the quotes, the following code can be used:

$1:

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 is the best way to merge two array files together using jq?

Imagine having file one: [1, 1, 2] then file two: [2, 3, 3] Is there a way to combine these arrays from the two files without altering their content? I'm looking for an output like this: [1, 1, 2, 2, 3, 3] On a side note, I am interested in concate ...

using a route object to update the path in Nuxt

I need to navigate to a specific URL: myApp.com/search-page?name%5Bquery%5D=value The code snippet below works perfectly when I'm on the homepage myApp.com: this.$router.push({ path: "search-page", query: { name: { query: `${this.value} ...

Attempting to output properties from an Express/Mongo API by utilizing a React.js frontend

I am currently in the process of developing a simplistic fictional sneaker application with the MERN stack. While I wouldn't classify myself as a beginner, I'm also not an expert. I successfully created the backend and generated a json rest-api. ...

Can't access innerText in Firefox

This is the code snippet I'm having trouble with: <div id="code">My program<br />It is here!</div> <script type="text/javascript"> var program=document.getElementById('code'); ShowLMCButton(program.innerText); </s ...

Is it possible for me to utilize window.top within the .js file?

Within my GWT code, I am transferring a variable to a JSP file. The process looks like this: <html> <head> <script type="text/javascript"> alert("Inside the JSP file"); var criticalPath = window.top.criticalPath; ...

Successfully Determining User Identity with Ajax Authentication

Currently, I am facing a security issue with my login page that uses an Ajax request for user authentication. The password entered by the user is sent as plain text in the form data of the Ajax request, making it vulnerable to interception by sniffing tool ...

Utilize Sorting while Keeping the Original Keys

Is there another method I can use instead of the sort function in php that won't delete all the keys? ...

Angular JS appears to be failing to properly establish values in the Dropdownlist

I have a project requirement to connect a dropdownlist with MVC and angular JS. Here is my attempt: var app1 = angular.module('Assign', []) app1.controller('SAPExecutive_R4GState', function ($scope, $http, $window) { // alert(UMS ...

Is the runTest.ts class in the vscode-test setup ever utilized in the project? Its purpose remains unclear even in the example project

Being a novice to Typescript, JavaScript, and VScode Extensions I have set up a vscode-test following the guidelines provided here: https://code.visualstudio.com/api/working-with-extensions/testing-extension#custom-setup-with-vscodetest Based on the hel ...

The current error message states that the function is undefined, indicating that the Bookshelf.js model function is not being acknowledged

I have implemented a user registration API endpoint using NodeJS, ExpressJS, and Bookshelf.js. However, I encountered an error while POSTing to the register URL related to one of the functions in the User model. Here is the code snippet from routes/index. ...

Make sure to blur all images whenever one of them is clicked

I am currently facing an issue with my webpage where I have 3 images displayed. I have implemented an event listener to detect clicks on the images, and once a click occurs on one of them, I want everything else on the page to become blurred, including the ...

Tips for integrating execute_script and WebDriverWait in Selenium automation

Is there a way to combine execute_script() and WebdriverWait? In my current code: network_list = driver.find_element_by_xpath('//*[@id="folder_box"]/div[1]/div/div[2]/div[1]') wait = WebDriverWait(driver, 4) try: wait_network_list = wait.unt ...

Unable to bring in Vue component from NPM module

Hello there, I recently developed my own npm package for a navigation bar and I need to incorporate it into my main codebase. Currently, I am utilizing vue @components but I am struggling with integrating the imported component. If anyone has insight on h ...

How can I transfer form data to a PHP variable using an AJAX request?

Encountering some difficulties, any insights? I have included only the necessary parts of the code. Essentially, I have an HTML form where I aim to extract the value from a field before submission, trigger an ajax call, and fill in another field. It seems ...

Loading modules conditionally in Nuxt.js

In my Nuxt.js configuration, I have included a module for Google Tag Manager like this: modules: [ [ '@nuxtjs/google-tag-manager', { id: 'GTM-XXXXXXX' } ] ] Everything is functioning properly, but I am curious ab ...

Contrasting actions observed when employing drag functionality with arrays of numbers versus arrays of objects

Being a newcomer to D3 and JavaScript, I'm hoping someone can help me clarify this simple point. I am creating a scatter graph with draggable points using code that closely resembles the solution provided in this Stack Overflow question. When I const ...

Unleashing the Power of Python for Extracting Data from Websites Using JSON Technology

I'm attempting to retrieve the price of a single item from a specific website, but I'm encountering difficulties when examining the page source. The URL in question is: I am particularly interested in this part of the page source (I assume): &l ...

Query parameter is not defined

Can anyone assist me with extracting the ean from the following URL: This NodeJS server processes the request in the following manner: const http = require('http') const port = 3000 const requestHandler = async (request, response) => { ...

Loop through the JSON data to obtain distinct values for certain indices

My PHP script retrieves data with the following query: SELECT objective,signal_type,signal_name FROM signals WHERE channel="Email" This is how the data is returned: [ { "objective": "Awareness", "signal_type": "Efficiency", " ...

What is the best way to retrieve a file's creation date using the file System module in Node.js

I'm having trouble fetching the filename and file creation date to send to a client. I tried using fs.stat which provides birthtime but does not include the filename. So, my question is: is birthtime equivalent to the file created date? How can I sen ...