json content random key in hash, how to use json schema spec in this case

622 views
Skip to first unread message

tiger63...@gmail.com

unread,
Apr 22, 2015, 10:57:19 AM4/22/15
to json-...@googlegroups.com
Hi guys, 
I have a problem.
i m working on ruby on rails and i have a big json to check.
I want to use jSON Schema to check my json in my unit test.


It almost works but one thing is bloking me... :(

I have a json like that
{
"myjson": {
            "af": {
              "userId": -1,
              "username": "",
              "progress": 0,
              "timestamp": "1"
            },
            "am": {
              "userId": -1,
              "username": "",
              "progress": 0,
              "timestamp": "1"
            },
....
}, 'toto':'value' ...
}


The key on myjson object can be af or am , or another thing not really determinated.

When i try to use : http://jsonschema.net/#/

 The schema is like that : 
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "/",
  "type": "object",
  "properties": {
    "data": {
      "id": "data",
      "type": "object",
      "properties": {}
    },
    "myjson": {
      "id": "myjson",
      "type": "object",
      "properties": {
        "af": {
          "id": "af",
          "type": "object",
          "properties": {
            "userId": {
              "id": "userId",
              "type": "integer"
            },
            "username": {
              "id": "username",
              "type": "string"
            },
            "progress": {
              "id": "progress",
              "type": "integer"
            },
            "timestamp": {
              "id": "timestamp",
              "type": "string"
            }
          }
        },
        "am": {
          "id": "am",
          "type": "object",
          "properties": {
            "userId": {
              "id": "userId",
              "type": "integer"
            },
            "username": {
              "id": "username",
              "type": "string"
            },
            "progress": {
              "id": "progress",
              "type": "integer"
            },
            "timestamp": {
              "id": "timestamp",
              "type": "string"
            }
          }
        }
      }
    }
  },
  "required": [
    "data",
    "myjson"
  ]
}

How can i have just one generic object  and ovoid to have "am", "af".... ?

I hope it s understanding.
Cheers,

Thomas

 

Jason Desrosiers

unread,
Apr 23, 2015, 12:37:26 AM4/23/15
to json-...@googlegroups.com, tiger63...@gmail.com
You can use `additionalProperties` if the key can be any string.
{
  "type": "object",
  "properties": {
    "data": { "type": "object" },
    "myjson": {
      "type": "object",
      "additionalProperties": {
        "type": "object",
        "properties": {
          "userId": { "type": "integer" },
          "username": { "type": "string" },
          "progress": { "type": "integer" },
          "timestamp": { "type": "string" }
        }
      }
    }
  },
  "required": ["data", "myjson"]
}


If the key has constraints you can use `patternProperties` instead.  For example, if the key is always two lowercase letters, you could use the following.
{
  "type": "object",
  "properties": {
    "data": { "type": "object" },
    "myjson": {
      "type": "object",
      "patternProperties": {
        "^[a-z][a-z]$": {
          "type": "object",
          "properties": {
            "userId": { "type": "integer" },
            "username": { "type": "string" },
            "progress": { "type": "integer" },
            "timestamp": { "type": "string" }
          }
        }
      },
      "additionalProperties": false
    }
  },
  "required": ["data", "myjson"]
}


Also, you are using `id` incorrectly.  I think you want to use `title` instead.
Reply all
Reply to author
Forward
0 new messages