Is there a standard for key naming of a JSON?

Asked

Viewed 428 times

0

I am consuming a JSON, and I want to use the keys of this JSON as options of a select, but the nomenclature is in camelCase and without accents, ex:

    "destaques": [
      {
        "fornecedoresCompany": [
          {
            "5OouMaisFuncionarios": "value..."
          }
        ]
      },
    ]

Is there a problem if I use it as follows?

    "Destaques": [
      {
        "Fornecedores Company": [
          {
            "5O ou Mais Funcionários": "value..."
          }
        ]
      },
    ]

If it is not correct to use in this way, is there any other solution? With javascript? I need to use the key as an option for it to be dynamic, it is no use only to perform a validation in the code comparing and changing the string.

  • The problem of variables with space is if you need to deserialize, no language accepts space in the nomenclature of variables and you will have to do a complex mapping to solve this. Accents are accepted, but in some cases they can be read differently in different editors. (encoding problem)

  • Related: https://answall.com/q/313493/64969

1 answer

2

There are no limitations to the JSON key, it is a string.

Section 7 of the RFC defines what is string, while the section 4 defines that a member is composed of <string> <separador> <valor>, therefore defines that the JSON key is a generic string.

Note that in some cases a JSON object (not to be confused with JSON array) will be deserialized as a map, and this does not change at all the fact that spaces are used in the name of the keys. But there are also some frameworks and libraries that try to associate the name of the key with that of some property of objects, in these cases you would have problem.

It seems to me you have control over the production of JSON. In this case, maybe you could enter the desired display names not in the key, but as some JSON member’s value. Without greater contexts it becomes difficult for me to try to come up with some significant example:

{ "destaques":
  { "nomeExibir": "Destaques",
    "valores":
    [
      { "fornecedoresCompany":
        { "nomeParaExibir": "Fornecedores Company",
          "valores":
          [
            { "nomeParaExibir": "50 ou mais funcionários",
              "valor": "value..."
            }
          ]
        }
      }
    ]
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.