Change Key in JSON, when DASH "-" is present in key – nodeJS or JavaScript

Summary:
I have no idea how to rename key that has dash in it. for example

obj.Fast-Italian = obj.Fast-Car1;

While below code works for keys that doesn’t contain dash "-":

var json = '[{"_id":"5078c3a803ff4197dc81fbfb","email":"user1@gmail.com","image":"some_image_url","name":"Name 1"}]';

var obj = JSON.parse(json)[0];
obj.id = obj._id;
delete obj._id;
 
json = JSON.stringify([obj]);
fs.writeFileSync('output1.json', json);

I can’t use above for this JSON:

var json = '[{"Fast-Car1":"Ferrari F40 Cabrio","Fast-Car2":"Audi R8 Black Edition","Fast-Car3":"Nissan GTR Blue"},{"Fast-Car1":"Lambo Diablo Fire Colors","Fast-Car2":"Skoda RS 4 doors","Fast-Car3":"Honda NSX red paint"}]'
    // what I need to go here is change Fast-Car1 Fast-Italian, Fast-Car2 = Fast-German, Fast-Car3 = Fast-Japanese

Problem is I don’t know how to make this work:

obj.Fast-Italian = obj.Fast-Car1;

due to dash "-" in Key name.

    //so final JSON would look like this:
    var json = '[{"Fast-Italian":"Ferrari F40 Cabrio"},{"Fast-German":"Audi R8 Black Edition"},{"Fast-Japanese":"Nissan GTR Blue"},,{"Fast-Italian":"Lambo Diablo Fire Colors","Fast-German":"VW Golf RS silver","Fast-Japanese":"Honda NSX red paint"}]'
    // JSON has big amount of those, so I will loop anyway, but I have no idea how to 
     

I tried this :

var obj = JSON.parse(json)[0];
obj.[Fast-Italian] = obj.['Fast-Car1'];
delete obj._id;

but then got error:

// obj.id = obj.['Fast-Car1'];
// SyntaxError: Unexpected token '['

Extra note:
JSON comes from Excel where each column has Fast-[Something] (in case you were wondering why I have JSON keys with dash "-")

70 thoughts on “Change Key in JSON, when DASH "-" is present in key – nodeJS or JavaScript”

  1. Pingback: sildenafil coupon
  2. Pingback: viagra generic
  3. Pingback: cialis on line
  4. Pingback: get cialis
  5. Pingback: sildenafil 50 mg
  6. Pingback: natural viagra

Leave a Comment