According to recommendation on JSON API specification site, we should use all lower case member names in JSON separated by hyphens:
The allowed and recommended characters for an URL safe naming of members are defined in the format spec. To also standardize member names, the following (more restrictive) rules are recommended:
Member names SHOULD start and end with the characters "a-z" (U+0061 to U+007A) Member names SHOULD contain only the characters "a-z" (U+0061 to U+007A), "0-9" (U+0030 to U+0039), and the hyphen minus (U+002D HYPHEN-MINUS, "-") as separator between multiple words.
So basically, we should be using JSON like this:
{
"first-name": "Jason",
"last-name": "Tough"
}
Would not it make it cumbersome to access those properties in JavaScript? Or any other programming language for that matter, especially if we want to generate classes from JSON Schema?
What is the motivation behind this recommendation?
Hyphens no longer allowed but JSON API recommends them by default #206.
JSON:API is a specification for how a client should request that resources be fetched or modified, and how a server should respond to those requests. JSON:API is designed to minimize both the number of requests and the amount of data transmitted between clients and servers.
JSON API is a format that works with HTTP. It delineates how clients should request or edit data from a server, and how the server should respond to said requests.
That being said, both GraphQL and JSON:API are compatible with REST, and while the Venn Diagram of offerings that are “REST and also GraphQL” is not 100% (and neither is the case with JSON:API), the tooling and community is still very large.
if you use hyphens, they can get treated as "minus" unless you bracket them - unaesthetic and extra typing... var t = json.phrase_stems.text-1; //error var t = json.phrase_stems["text-1"]; //ok
These recommendations are intended to establish a level of consistency in areas that are beyond the scope of the base JSON:API specification. The specification places certain hard restrictions on how members (i.e., keys) in a JSON:API document may be named.
What Is a Hyphen? A hyphen (-) is a punctuation mark that’s used to join words or parts of words. It’s not interchangeable with other types of dashes. Use a hyphen in a compound modifier when the modifier comes before the word it’s modifying. If you’re not sure whether a compound word has a hyphen or not, check your preferred dictionary.
JSON:API Object. A JSON:API document MAY include information about its implementation under a top level jsonapi member. If present, the value of the jsonapi member MUST be an object (a “jsonapi object”). The jsonapi object MAY contain a version member whose value is a string indicating the highest JSON API version supported.
Looks like JSONAPI naming from the quote in question is no longer valid. Currently the `JSONAPI recommends:
Naming
The specification places certain hard restrictions on how members (i.e., keys) in a JSON:API document may named. To further standardize member names, which is especially important when mixing profiles authored by different parties, the following rules are also recommended:
Member names SHOULD be camel-cased (i.e., wordWordWord) Member names SHOULD start and end with a character “a-z” (U+0061 to U+007A) Member names SHOULD contain only ASCII alphanumeric characters (i.e., “a-z”, “A-Z”, and “0-9”)
so above example should be:
{
"firstName": "Jason",
"lastName": "Tough"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With