1

I have this array of javascript objects (which are actually musical notes) that are generated in my javascript front-end. In order to store it, I had to run javascript JSON.stringify(objectArray)before throwing it into a hidden input, but that automatically encases all my keys in double quotes like so:

[
  {"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 my params in Rails, I run JSON.parse(params[:score][:notes]) to turn it from a string into a proper JSON array for storage in MongoDB (I'm specifically using Mongoid)

I know it's normally proper procedure to keep keys in quotes in most cases, but I like using the dot notation for keys to grab values in all my JS.

Should I switch my JS to reference everything with brackets, or can you think of a simple function that would quickly parse out the keys' quotes before sending to the hidden input?

Cjmarkham
  • 9,484
  • 5
  • 48
  • 81
  • possible duplicate of [JSON Spec - does the key have to be surrounded with quotes?](http://stackoverflow.com/questions/949449/json-spec-does-the-key-have-to-be-surrounded-with-quotes) – infused Jan 14 '15 at 21:28
  • I don't know understand well the reason but you can remove the quotes whit a simple replace like `varStringified.replace(/\"/g, "");` This is solve your answer? – Nothing Jan 14 '15 at 21:29
  • 3
    The quotes will not prevent you from using dot notation. – Travis J Jan 14 '15 at 21:30
  • infused - Thanks, I did read that one. Nothing - This take out all the double quotes, they need to remain for all values. I'm just trying to remove them from the keys. Travis, I tested and I think you're right on this one. So I'm guessing the brackets are really just good to throw in a variable or something as the key name. Thanks! – Andrew Appleby Jan 14 '15 at 22:29

1 Answers1

2

I was looking to do something similar to pass linting, and this regex finds all quotes before semicolons in the string:

"(\w*)":

And replace that with just the text, excluding the quotes:

$1:

Methos
  • 132
  • 1
  • 8