4

I need to create a JSON schema for object that will include java Properties object as one of its properties. The nested Properties object will be simply list of key=value. Both key and value are of type string. I failed to find any docs that describe how to define the schema that includes 2 new types.

shall it be something like:

{
"type": "object",
"name": "MyObj",
"properties": {
    "prop1": {
        "type": "string",
        "description": "prop1",
        "required": true
    },
    "props": {
        "type": "array",
        "items": {
            "type": "object"
            "properties": {
                "key": {
                    "type": "string",
                    "description": "key",
                    "required": true
                },
                "value": {
                    "type": "string",
                    "description": "the value",
                    "required": true
                }
            }
            "description": "the value",
            "required": true
        }
    }
}

}

2 Answers 2

23

The schema you have written (assuming the commas are fixed) describes data of the form:

{
    "prop1": "Some string property goes here",
    "props": [
        {"key": "foo", "value": "bar"},
        {"key": "foo2", "value": "bar2"},
        ...
    ]
}

If this is what you wanted, then you are already finished.

However, I do wonder why you are using key/value pairs in an array, when you could use a JSON object with string keys instead. Using the additionalProperties keyword, you could have a schema:

{
    "type": "object",
    "name": "MyObj",
    "properties": {
        "prop1": {
            "type": "string",
            "description": "prop1"
        },
        "props": {
            "type": "object",
            "additionalProperties": {
                "type": "string",
                "description": "string values"
            }
        }
    }
}

This describes a data format like:

{
    "prop1": "Some string property goes here",
    "props": {
        "foo": "bar",
        "foo2": "bar2"
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

If you want to impose constraints on the key, you can also use patternProperties with additionalProperties: false. You can use "pattern" to impose constraints on the value as well.
0

At W3 schools (JSON Syntax) you can read how the array should be defined.

There is no schema like the xsd for xml, however i've found an approach on json-schema.org. If you are able to, i'll advice to youse google-GSON library for JSON. You could Store key Value as "id" : "value" and build only one object, containing all requieed pairs:

{ "lang" : "EN" , "color" : "red" }

Your posted model is incorect, you can check it on jsonlint.com Here is a working version, i'm not sure if the modell is as expected.

{
    "type": "object",
    "name": "MyObj",
    "properties": [
        {
            "prop1": {
                "type": "string",
                "description": "prop1",
                "required": true
            },
            "props": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {
                        "key": {
                            "type": "string",
                            "description": "key",
                            "required": true
                        },
                        "value": {
                            "type": "string",
                            "description": "the value",
                            "required": true
                        }
                    },
                    "description": "the value",
                    "required": true
                }
            }
        }
    ]
}

1 Comment

Thanks, I fixed few commas that were missing, but I guess what I was trying to ask was : is there more common way to define java Properties-like member in JSON schema or should I stay with an array?

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.