Parse a JSON file with JSON.parse (); It turns out such a thing that the key with a hyphen does not work. For example, this key gives the error: "app-categoty": "Games"

If you change to: "appCategoty": "Games", "app_categoty": "Games" and even: "Game category": "Games" Works without problems.

In the specification, the restriction on the hyphen was not found. What could be the problem?

    1 answer 1

    Here's an example showing the fact that there can be properties with a hyphen in JSON and this does not contradict the documentation. But if you already access the property as objectFromJSON.app-category you will get an error, since the hyphen is reserved in JS. Therefore, you need to access this property as objectFromJSON['app-category']

    Consequently

     const data = { 'app-category': 'Игры' }; const JSONData = JSON.stringify(data); const objectFromJSON = JSON.parse(JSONData); console.log(objectFromJSON);