How to make a field unique in JSON schema

minh tran 216 Reputation points
2022-01-11T00:12:03.68+00:00

Hello,
I am studying JSON schema, and I would like to seek your help on how to make a field unique in JSON schema. I have a simple schema containing StudentID that I want to make it unique so there would not be duplicate JSON objects having same student ID.

My question is that can I enforce the uniqueness of the StudentID field by setting the uniqueItem equaled to true in my studentID property field as below in my JSON Schema? May you please help me to correct my mistake?

{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "$ref": "#/definitions/Welcome5",
  "definitions": {
    "Welcome5": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "StudentID": {
          "type": "integer",
          "min": 1,
          "uniqueitem": true
        },
        "name": {
          "type": "string"
        },
        "grade": {
          "type": "number"
        },
        "passed": {
          "type": "boolean"
        },
        "Classestaken": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "adolescence": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/Adolescence"
          }
        }
      },
      "required": [
        "Classestaken",
        "adolescence",
        "grade",
        "StudentID",
        "name",
        "passed"
      ],
      "title": "Welcome5"
    },
    "Adolescence": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "name": {
          "type": "string"
        },
        "Growup": {
          "type": "string"
        }
      },
      "required": [],
      "title": "Adolescence"
    }
  }
}
Azure Data Factory
Azure Data Factory
An Azure service for ingesting, preparing, and transforming data at scale.
11,625 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. MartinJaffer-MSFT 26,236 Reputation points
    2022-01-11T20:21:58.397+00:00

    Hello and welcome @minh tran . While your question is asking "how do I do ____" I'm afraid the reality isn't that simple. Please allow me the opportunity to do some education.

    UPDATE: In my haste I thought this was regular JSON data, not schema. I will need to do some self-learning. My response below is not with respect to schemas.
    You tagged this as azure-data-factory. Can you tell me how your question ties in to Data Factory, or is this just about JSON with no tie-in?

    JSON is a way of organizing and writing data. JSON in itself does not have means to enforce uniqueness, however properly written JSON should not have duplicate keys within the same scope and object. Enforcement of uniqueness should be done either by the writer or the reader.

    To better highlight what I mean, let's use a JSON validator. For expediency, I am using https://jsonlint.com/
    Examples below to be discussed:

    invalid, duplicate key:  
    {  
    	"a": 1,  
    	"a": 2  
    }  
      
    valid, duplicate items in array:  
    {  
    	"items": [  
    		"a",  
    		"a"  
    	]  
    }  
      
    valid, objects in array with duplicate "id" property intended as unique:  
    {  
    	"items": [{  
    			"id": 123,  
    			"value": "x"  
    		},  
    		{  
    			"id": 123,  
    			"value": "Q"  
    		}  
    	]  
    }  
    

    The first of these is technically wrong, but nothing is preventing you from writing that. This would be the closest to ensuring uniqueness. This is because when there are duplicate property keys, asking for their value becomes ambiguous -- which one. Quality JSON writers should implement checks to prevent this from happening.

    The middle example demonstrates how arrays have no requirement that the contents be unique. This is because the arrays have an order, you can ask for an item by index.

    In the bottom example, the "id" was intended to be unique by the user to make each object unique, but this is a fallacy. The keys must only be unique within the same scope. Here, each object is a separate scope, so the JSON is still valid, even if it doesn't do what the user intended.

    0 comments No comments

  2. Johnny Qian 0 Reputation points Microsoft Employee
    2023-08-01T07:15:57.35+00:00

    If you're looking for a method to detect the duplicate keys, then just use JObject.Parse method with specific parameter in Json.NET.

    try
    {
        var _= JObject.Parse(jsonString, new JsonLoadSettings
        {
            DuplicatePropertyNameHandling = DuplicatePropertyNameHandling.Error
        });
    }
    catch(JsonReaderException ex)
    {
        return BadRequest($"Invalid json file, {ex.Message}.");
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.