Skip to content
Advertisement

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

JavaScript

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

JavaScript

I can’t use above for this JSON:

JavaScript

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

JavaScript

due to dash “-” in Key name.

JavaScript

I tried this :

JavaScript

but then got error:

JavaScript

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

Advertisement

Answer

You were on the right track with:

JavaScript

There are 2 problems…
One, you dont mix and match dot notation and bracket notation. So dont do obj.[keyname] you just do obj[keyname].

Two, Fast-Italian isn’t a declared variable name from what I can see… its just supposed to be a string key name.. so you need to enclose it with quotes

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement