2

I want to parse json, based on the following classes:

public class DerModel
{
    public string Name { get; set; }
    public string Email { get; set; }
}

public class DriverPositiveResultModel
{
    public int DriverId { get; set; }
    public string DriverName { get; set; }
    public string DriverSSN { get; set; }
    public string CarrierName { get; set; }
    public DerModel DER { get; set; }
}

and the following schema:

{
    "properties": {
        "CarrierName": {
            "type": "string"
        },
        "DER": {
            "properties": {
                "Email": {
                    "type": "string"
                },
                "Name": {
                    "type": "string"
            }
        },
        "type": "object"
    },
    "DriverId": {
        "type": "integer"
    },
    "DriverName": {
        "type": "string"
    },
    "DriverSSN": {
        "type": "string"
    }
},
"type": "object"

}

but logic allows, that DER can be null. How to set it in the schema?

1 Answer 1

9

You need to specify that it can be null:

"type": ["object","null"]

so your code would look like this:

{
    "properties": {
        "CarrierName": {
            "type": "string"
        },
        "DER": {
            "properties": {
                "Email": {
                    "type": "string"
                },
                "Name": {
                    "type": "string"
            }
        },
        "type": ["object","null"]
    },
    "DriverId": {
        "type": "integer"
    },
    "DriverName": {
        "type": "string"
    },
    "DriverSSN": {
        "type": "string"
    }
},
"type": "object"
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.