Is there any way to validate uniqueness of the object properties with JSON schema?

2,340 views
Skip to first unread message

th...@drminside.com

unread,
Jul 25, 2015, 3:12:38 AM7/25/15
to json-...@googlegroups.com
Hello ..

According JSON specification Draft v4, it is possible to validate uniqueness of item in an array data by "uniqueItems" keyword.
But I could not find any way in object properites case.

Actually, I would like to validate the uniqueness of following JSON object;

{
    "id" : "182738492-1029384",
    "data1" : "...",
    "data2" : "...", 
    ...,
    "id" : "xxx738492-xxx9384"
}

I had tried to make a proper schema with which JSON parser could validate the second "id" property not to occur inadvertently again.

But I could not succeed to find proper way in the schema level.

Is it impossible to validate the duplication check for the object properties with JSON schema?


Joe McIntyre

unread,
Jul 25, 2015, 1:40:05 PM7/25/15
to JSON Schema, th...@drminside.com
The issue is more relevant to JSON, since JSON Schema is dependent on the service of the JSON parser to present the content.

In the JSON specification, the use of duplicate names is discouraged, and as clarified in RFC 7159, interoperability is dependent on this being the case. Thus the duplicate name condition should be handled by the JSON parser, and your application should interact with the JSON parser to address the desired behavior, since the JSON parser will be making the decision on how to handle the JSON content before the schema comes into play.
 
The text from RFC 7159 (JSON March 2014) follows. RFC 4627 (JSON July 2006) includes the same first paragraph, but does not contain the clarifying second paragraph on interoperability.

4. Objects

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. object = begin-object [ member *( value-separator member ) ] end-object member = string name-separator value An object whose names are all unique is interoperable in the sense that all software implementations receiving that object will agree on the name-value mappings. When the names within an object are not unique, the behavior of software that receives such an object is unpredictable. Many implementations report the last name/value pair only. Other implementations report an error or fail to parse the object, and some implementations report all of the name/value pairs, including duplicates.

 
Reply all
Reply to author
Forward
0 new messages