Do JSON Keys Need to Be Unique

Do JSON keys need to be unique?

There is no "error" if you use more than one key with the same name, but in JSON, the last key with the same name is the one that is going to be used.

In your case, the key "name" would be better to contain an array as it's value, instead of having a number of keys "name". It doesn't make much sense the same object or "thing" to have two names, or two of the same properties that are in conflict.

E.g.:

{
"name" : [ "JOHN", "JACK", "...", ... ]
}

Does JSON syntax allow duplicate keys in an object?

From the standard (p. ii):

It is expected that other standards will refer to this one, strictly adhering to the JSON text format, while
imposing restrictions on various encoding details. Such standards may require specific behaviours. JSON
itself specifies no behaviour.

Further down in the standard (p. 2), the specification for a JSON object:

An object structure is represented as a pair of curly bracket tokens surrounding zero or more name/value pairs.
A name is a string. A single colon token follows each name, separating the name from the value. A single
comma token separates a value from a following name.

Diagram for JSON Object

It does not make any mention of duplicate keys being invalid or valid, so according to the specification I would safely assume that means they are allowed.

That most implementations of JSON libraries do not accept duplicate keys does not conflict with the standard, because of the first quote.

Here are two examples related to the C++ standard library. When deserializing some JSON object into a std::map it would make sense to refuse duplicate keys. But when deserializing some JSON object into a std::multimap it would make sense to accept duplicate keys as normal.

Do the JSON keys have to be surrounded by quotes?

Yes, you need quotation marks. This is to make it simpler and to avoid having to have another escape method for javascript reserved keywords, ie {for:"foo"}.

Find unique values in list of JSON objects

For your first question, you can use data model like a Map<String, Set> where the key is the JSON key and the Set will take all your JSON values. Duplicates will be ignored as that's how the Set data model works. Then, simply iterate through your JSON objects and their properties and populate your Map.

For your second question, you can use Map<String, Map<String, Integer>. As you iterate simply get (or create, if not found) record from the first map using the JSON key, and then get (or create, if not found) the record from the inner map by the JSON property value. Increment the Integer by 1 in the record existed, otherwise use value 1.

Can json have only key without value?

The original JSON introduction is extremely straight-forward, including diagrams of the possible constructions. Among the text content is this:

An object is an unordered set of name/value pairs. An object begins with { left brace and ends with } right brace. Each name is followed by : colon and the name/value pairs are separated by , comma.

The later ECMA-404 standard includes similar wording:

An object structure is represented as a pair of curly bracket tokens surrounding zero or more name/value pairs. A name is a string. A single colon token follows each name, separating the name from the value. A single
comma token separates a value from a following name.

The IETF standard, RFC 8259 puts it this way:

An object structure is represented as a pair of curly brackets
surrounding zero or more name/value pairs (or members). A name is a
string. A single colon comes after each name, separating the name
from the value. A single comma separates a value from a following
name. The names within an object SHOULD be unique.

In every case, note that every key is followed by a colon and a value. Since there is no colon following the key in your example, it is not valid JSON.

Need all the unique Keys name from Json string

You just need to flatten the object and filter accordingly:

var uniqueKeys = json.Descendants()
.OfType<JProperty>()
.Where(x => x.Value is JValue)
.Select(x => x.Name)
.Distinct();


Related Topics



Leave a reply



Submit