Question

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
        }
    }
}

}

Was it helpful?

Solution

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"
    }
}

OTHER TIPS

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
                }
            }
        }
    ]
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top