-1

I got a json and it has special characters inside.

myJSON = "metadata": Object {
  "album-art": "album-art.png",
}

However - Variable declaration is not possible due to the special character "-".

const Variable = myJSON.album-art

Can I put it inside a variable?

thisofox
  • 13
  • 3
  • 2
    you can do `myJSON["album-art"]` – Charlo Poitras May 26 '22 at 15:46
  • Also: [How do I reference a JavaScript object property with a hyphen in it?](https://stackoverflow.com/questions/7122609/how-do-i-reference-a-javascript-object-property-with-a-hyphen-in-it) – Idrizi.A May 26 '22 at 15:50

2 Answers2

0

Do this. There are two methods to access a property/method in a object. One is dot notation and the other is square bracket notation (example below)

const Variable = myJSON['album-art']

Ryker
  • 792
  • 4
  • 11
0

It's a better practice to use _ instead of -, but you can access the variable using myJSON["prop"]

myJSON = {
  "album-art": "album-art.png",
}

console.log(myJSON["album-art"])
Charlo Poitras
  • 198
  • 1
  • 14