admin管理员组

文章数量:1320780

I have a object something like:

{
    "category|subCategory" : value
}

Is it wrong to use "|" (which I intend to use as a delimiter) in key of an object?

I have a object something like:

{
    "category|subCategory" : value
}

Is it wrong to use "|" (which I intend to use as a delimiter) in key of an object?

Share Improve this question asked Sep 28, 2017 at 6:42 rudy0807rudy0807 1521 silver badge8 bronze badges 4
  • 3 It is neither "right" nor "wrong". It is valid, and that's all that really matters – Mulan Commented Sep 28, 2017 at 6:43
  • 2 Yes it's valid. But you won't be able to access the object property by dot notation. – abhishekkannojia Commented Sep 28, 2017 at 6:45
  • Yes, I am using the property using ["category|subCategory"], so thanks for the ment :) – rudy0807 Commented Sep 28, 2017 at 6:52
  • Separator characters are normally chosen from the set of characters that are not already being used. Pipe is often a good character for this in many data sets. Looking at mon Redis use, which uses this paradigm a lot to form posite keys, the pipe char and the colon appear most often. Colon is very popular for this purpose as it has a similar language meaning but it's only usable if you're sure you won't find colon appearing as valid characters in the individual keys. – NeilG Commented Dec 11, 2023 at 13:42
Add a ment  | 

2 Answers 2

Reset to default 6

It is valid. Property names may be any string.

Wrongness seems like a moral judgement that is a matter of opinion.

According to the standard, any string can be used as the key.

A string is a sequence of Unicode code points wrapped with quotation marks (U+0022). All code points may be placed within the quotation marks except for the code points that must be escaped: quotation mark (U+0022), reverse solidus (U+005C), and the control characters U+0000 to U+001F.

Even {"⛄|⛱|☠": "is valid"} is valid.

本文标签: javascriptIs it wrong to use Pipe symbol () in a json keyStack Overflow